Now lets continue with our game. In the init function remove the hello world code and add this row:
startGame();
and right afrer the init function we will add startGame:
private function startGame():void { this.addEventListener(Event.ENTER_FRAME, gameLoop); }And now the game loop:
private function gameLoop(event:Event = null):void { }
It is time to add the player. Lets make new folder called assets and add some images to it. For player spaceship I’ll use this cool image:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEizfwcvXz9kTZzEu4MyHLAO9cR8HipZ4VcvHRirYZpm2dbT-SLkkpe9YFhRmW87b6w5cUE3ZanQIuDVWIc7bhQV4JHk72P7u8j0mq8lrSCYm2Gl9RmMbQb8s0iLQtJsTS_6XOPhP1PJUw/s320/s1.png)
and for enemies this one:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgnFz83_wehfP_v0TWYaaTamyBuNHWevH1TNdFn0LqOIcZFd__jLrCds-SvMmEG7dkVRLr-mZuPfxqf0rBSiTEzHgXo70ERBKWEVw8KMfN2_9GnVG3gE_kHLfNov1gdsR6wrbQhvGghBA/s320/ship1greensmall.png)
You can use my images but please credit me.
Lets start with adding the player. More about embedding images you can learn from this post - FlashDevelop, ActionScript3 and images.
Add this in the class body:
[Embed(source="../assets/s1.png")] private var playerShipImage:Class; private var player:MovieClip;
And now alter the startGame function this way:
private function startGame():void { player = new MovieClip(); var bitmap:Bitmap = new playerShipImage(); bitmap.x = -bitmap.width / 2; bitmap.y = -bitmap.height / 2; player.addChild(bitmap); player.x = 250; player.y = 430; addChild(player); this.addEventListener(Event.ENTER_FRAME, gameLoop); }Compile and run! Here is the result:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjgopip1sSr4MIYh1Gob0_1HHjCTZqDTtowvzMS_YtXP4cXmHN2Il6rY9Ge0JkF_XgDF3lnA7_aMVwUZEgLjiRCafLbOir2I26BCeIN1F_og_-mBYYPHnNIOJJhmnWW5PN_m-dyB64Ilw/s320/shooter1.png)
And here is the full source code till now:
Main.as
package { import flash.display.*; import flash.events.*; public class Main extends Sprite { [Embed(source="../assets/s1.png")] private var playerShipImage:Class; private var player:MovieClip; public function Main():void { if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init); } private function init(e:Event = null):void { removeEventListener(Event.ADDED_TO_STAGE, init); // entry point startGame(); } private function startGame():void { player = new MovieClip(); var bitmap:Bitmap = new playerShipImage(); bitmap.x = -bitmap.width / 2; bitmap.y = -bitmap.height / 2; player.addChild(bitmap); player.x = 250; player.y = 430; addChild(player); this.addEventListener(Event.ENTER_FRAME, gameLoop); } private function gameLoop(event:Event = null):void { } } }Part 1
Loved the tutorial and blog.... Keep up sharing....
ReplyDeleteRegards,
GamyGuru (http://gamyguru.wordpress.com)
great tutorial. when is part 3 coming?
ReplyDelete