Basic Actionscript 3 Game Part 1

A video tutorial showing how to create a basic game using Actionscript 3.

The first thing which we do is create two movieclips, one called player and one called bullet then we remove them from our stage by pressing the delete key on your keyboard.

Comment 1
var Player:player = new player(); This creates a new instance of the movieclip which we made called player.
Player.x = mouseX; This sets the X position of Player to the X position of the mouse.
Player.y = mouseY; This sets the Y position of Player to the Y position of the mouse.
addChild(Player); This adds the Player instance that we have just created to the stage.

Comment 2
stage.addEventListener(MouseEvent.MOUSE_MOVE, mousemove); An event listener listens out for an event which has been defined within the paranthesis. Here when the mouse moves on the stage then the function mousemove is called.
stage.addEventListener(MouseEvent.MOUSE_DOWN, shoot); When the left mouse button is pressed then the shoot function is called.

Comment 3
function mousemove(e:MouseEvent):void{, This creates the function mousemove and looks for mouse events.
Player.x = mouseX; Update the Player X position to the mouses X when the mouse is moved.
Player.y = mouseY; Update the Player Y position to the mouses Y when the mouse is moved.
} Close the function.

Comment 4
function shoot(e:Event):void{ create a function called shoot.
var Bullet:bullet = new bullet(); This creates a new instance of the bullet movieclip.
Bullet.x = Player.x; The Bullet X position is set to the same as the Player X position.
Bullet.y = Player.y; The Bullet Y position is set to the same as the Player Y position.
addChild(Bullet); Add the instance Bullet to the stage.
Bullet.addEventListener(Event.ENTER_FRAME, moveBullet); Add an event listener to Bullet for when it enters the frame and when it does call the function moveBullet.
} Now we close the function.

Comment 5
function moveBullet(e:Event):void{ create a function called moveBullet.
e.target.y -= 5; When the function is called the targets Y position will be subract by 5 pixels every frame, this makes the movieclip move up. The target is the Bullet movieclip.
if(e.target.y <= -10){ If the Y of Bullet is less than or equal to minus 10 then the event listener will be removed and so will the movieclip.
e.target.removeEventListener(Event.ENTER_FRAME, moveBullet);
removeChild(MovieClip(e.target));
} Close the function

Thanks for watching the tutorial and reading through the notes. If there is anything that you don’t understand then please post a omment and I will try to explain your problem as best as I can.

Bookmark and Share
Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • email
  • FriendFeed
  • LinkedIn
  • Ping.fm
  • Tumblr
  • Twitter

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

, , , , , , , , , , , , , ,

21 Responses to Basic Actionscript 3 Game Part 1

  1. sstng August 6, 2008 at 10:49 am #

    Hi, your tutorial is great but is there any chance which you know how to create a timer for the game ? A timer which sort of limit the game, for e.g Level 1 of the game is limited to 100 seconds, time up and its over.

    Could you help me out with that ? thanks

  2. Wez August 6, 2008 at 11:13 am #

    Yeah sure I'll write a tutorial on it so check back soon.

  3. Maxla August 20, 2008 at 7:39 am #

    thx for this great tutorial but one question how can i create a kind of live bar (you know what i mean?)

  4. marino August 23, 2008 at 4:48 am #

    Hi!
    Is there any other way to control the bullet from the function "shoot" other than "e.target"?

    I am asking this because I create the bullets the way you explained, and I also create an enemies the same way, but I don't know how to make the enemy and the bullet disappear when they collide...

    This is the entire code:

    var player:player_mc = new player_mc();
    addChild(player);
    var enemyTimer:Timer = new Timer(2000, 1); // when the timer ends, a new enemy is created
    enemyTimer.start();

    stage.addEventListener(Event.ENTER_FRAME, movePlayer);
    stage.addEventListener(MouseEvent.CLICK, createBullet);
    enemyTimer.addEventListener(TimerEvent.TIMER, createEnemy);

    function movePlayer(e:Event):void{
    player.y = mouseY;
    player.x = mouseX;
    }

    function createBullet(event:MouseEvent):void{
    var bullet:bullet_mc = new bullet_mc;
    bullet.y = mouseY - 25;
    bullet.x = mouseX;
    addChild(bullet);

    bullet.addEventListener(Event.ENTER_FRAME, moveBullet);
    }

    function moveBullet(e:Event):void{
    e.target.y -= 10;
    if(e.target.y = 400){
    e.target.removeEventListener(Event.ENTER_FRAME, moveEnemy);
    removeChild(MovieClip(e.target));
    }
    }

    Please help!
    I can also send you the .fla file if you wish :)

  5. marino August 23, 2008 at 4:51 am #

    oh! sorry! this is the entire code:

    var player:player_mc = new player_mc();
    addChild(player);
    var enemyTimer:Timer = new Timer(2000, 1); // when the timer ends, a new enemy is created
    enemyTimer.start();

    stage.addEventListener(Event.ENTER_FRAME, movePlayer);
    stage.addEventListener(MouseEvent.CLICK, createBullet);
    enemyTimer.addEventListener(TimerEvent.TIMER, createEnemy);

    function movePlayer(e:Event):void{
    player.y = mouseY;
    player.x = mouseX;
    }

    function createBullet(event:MouseEvent):void{
    var bullet:bullet_mc = new bullet_mc;
    bullet.y = mouseY - 25;
    bullet.x = mouseX;
    addChild(bullet);

    bullet.addEventListener(Event.ENTER_FRAME, moveBullet);
    }

    function moveBullet(e:Event):void{
    e.target.y -= 10;
    if(e.target.y = 400){
    e.target.removeEventListener(Event.ENTER_FRAME, moveEnemy);
    removeChild(MovieClip(e.target));
    }
    }

    I did a mistake in the previous post... :D

  6. marino August 23, 2008 at 4:54 am #

    what the hell?
    why isn't the whole code shown?
    I pasted it but a part is missing!!!

    I'll paste only the missing part:

    function createEnemy(event:TimerEvent):void{
    var enemy:enemy1_mc = new enemy1_mc();
    enemy.x = mouseX; // I don't know how to create random numbers so I improvised :(
    enemy.y = -15; // It looks more realistic if the enemies are created off-stage
    addChild(enemy);
    enemy.addEventListener(Event.ENTER_FRAME, moveEnemy);
    enemyTimer.reset(); // this resets the timer
    enemyTimer.start(); // when the timer ends, the function "creaateEnemy" is called again
    }

    function moveEnemy(e:Event):void{
    e.target.y += 5;
    if(e.target.y >= 400){
    e.target.removeEventListener(Event.ENTER_FRAME, moveEnemy);
    removeChild(MovieClip(e.target));
    }
    }

  7. Hells September 11, 2008 at 5:19 pm #

    Ok well to start off did what you said heres my code

    //comment 1
    var Player:player = new player();
    Player.x = mouseX;
    Player.y = mouseY;
    addChild(Player);

    //comment 2
    stage.addEventListener(MouseEvent.MOUSE_MOVE, mousemove);
    stage.addEventListener(MouseEvent.MOUSE_DOWN, shoot);

    //comment 3
    function mousemove(e:MouseEvent):void{
    Player.x = mouseX;
    Player.y - mouseY;
    }

    //comment 4
    function shoot(e:Event):void{
    var Bullet:bullet = new bullet();
    Bullet.x = Player.x;
    Bullet.y = Player.y;
    addChild(Bullet);
    Bullet.addEventListener(Event.ENTER_FRAME, moveBullet);
    }

    //comment 5
    function moveBullet(e:Event):void{
    e.target.y -=5;
    if(e.target.y <= -10){
    e.target.removeEventListener(Event.ENTER_FRAME, moveBullet);
    removeChild(MovieClip(e.target));
    }
    }

    The problem is when i preview the move i can only move side to side

  8. Hells September 11, 2008 at 5:20 pm #

    Hehe after reading my post over i finnaly spotted a - instead of a = fixed

  9. David September 12, 2008 at 10:47 am #

    You should create a tutoral combined with the key movement, and check what direction the player is in, basically you could just use a variable that changes up to 1 and down, left and right to 0, and then the bullet fires in the same direction as the last key pressed

  10. Ad January 7, 2009 at 8:53 pm #

    Hi I'm trying to create a simple drag and drop game that is played by 2 players on a single machine. I really want this to work on the application itself instead of a server. Can you please advise, my actionscipt knowledge isn't brilliant.

    Many thanks,

    Ad

  11. Neel February 7, 2009 at 9:01 pm #

    this is my code: (but it doesn't work, i don't know y can you help me plz, oh and i'm using cs4)

    //Comment 1
    var Player:player = new player();
    Player.x = mouseX;
    Player.y = mouseY;
    addChild(Player);

    //Comment 2
    stage.addEventListener(MouseEvent.MOUSE_MOVE, mousemove);
    stage.addEventListener(MouseEvent.MOUSE_DOWN, shoot);

    //Comment 3
    function mousemove(e:MouseEvent):void{
    Player.x = mouseX;
    Player.y = mouseY;
    }

    //Comment 4
    function shoot(e:Event):void{
    var Bullet:bullet = new bullet();
    Bullet.x = Player.x;
    Bullet.y = Player.y;
    addChild(Bullet);
    Bullet.addEventListener(Event.ENTER_FRAME, moveBullet); Add an event listener to Bullet for when it enters the frame and when it does call the function moveBullet.
    }

    //Comment 5
    function moveBullet(e:Event):void{
    e.target.y -= 5;
    if(e.target.y <= -10){
    e.target.removeEventListener(Event.ENTER_FRAME, moveBullet);
    removeChild(MovieClip(e.target));
    }

  12. one million euro blog February 9, 2009 at 12:44 pm #

    nice tutorial!

    we are waiting for the other parts, where there will be collisions between enemy and bullet.

    I think we will use hitTestObject property.

  13. Johannes March 2, 2009 at 4:58 pm #

    Really liked your tutorial. Hope to see some collisions added to the game soon.

  14. Jarff March 4, 2009 at 6:36 am #

    Hi, good tutorial and game!
    Is there a way of putting a button into this, so you can click on it? I've tried loading this into a .fla already, and works fine, just i can't click any of the other buttons on the page.

    Any help would be appreciated!

    Thanks - funny game btw.

  15. Sylario March 22, 2009 at 3:39 pm #

    Great tutorial! However it seems CS4 is more tough about objects.

    There is two errors about player.x and player.y :

    1119: Access of possibly undefined property x through a reference with static type Class. player.x=mouseX;
    1119: Access of possibly undefined property y through a reference with static type Class. player.y=mouseY;

    how can i correct that?

  16. Mokey August 30, 2009 at 10:41 am #

    Hey Sylario. You might not view this (Seems you've posted looong back ago) but it's hard to solve errors without the actual code itself. E-mail me your source at gbmokshal54@gmail.com and I'll take a look at it! This is usually an odd error and requires some time to look into...

  17. Mokey August 30, 2009 at 10:45 am #

    One more thing, mind if I suggest to use the: Mouse.hide(); statement? It'll make it look more neat...

  18. andy August 31, 2009 at 10:07 am #

    Hi, I loved the tutorial and have got to work perfectly with a fly shooting some rather than a square shooting a bullet box but the codes the same.
    One thing that is puzzling me that i can't work is every few seconds the movie clip leaves and instance of the fly behind. So after a while I've got heaps of them all over the screen.
    Please help.

    var Fly1:fly = new fly();
    //comment This creates a new instance of the movie clip fly to the variable Fly1
    Fly1.x = mouseX;
    Fly1.y = mouseY;
    addChild(Fly1);
    // comment This adds the variable containing the instance of the movie clip flip to the stage

    stage.addEventListener(MouseEvent.MOUSE_MOVE,mousemove);
    stage.addEventListener(MouseEvent.MOUSE_DOWN,poo);

    function mousemove(e:MouseEvent):void
    {
    Fly1.x = mouseX;
    Fly1.y = mouseY;

    };

    function poo(e:Event):void
    {
    var Fly_Poo1:fly_poo = new fly_poo;
    Fly_Poo1.x = Fly1.x-20;
    Fly_Poo1.y = Fly1.y-20;
    addChild(Fly_Poo1);
    Fly_Poo1.addEventListener(Event.ENTER_FRAME,makepoo);
    };

    function makepoo(e:Event):void
    {
    e.target.y -= 10;
    if(e.target.y <= -400){
    e.target.removeEventListener(Event.ENTER_FRAME, makepoo);
    removeChild(MovieClip(e.target));
    }
    };

  19. bronty May 29, 2010 at 4:49 am #

    how to make the bullet go to the left??

  20. bronty May 29, 2010 at 5:31 am #

    i mean how to make the bullet go to the right??
    and how to put in enemy??

Trackbacks/Pingbacks

  1. The Tutorial Blog » Blog Archive » Timers in Actionscript 3 - August 6, 2008

    [...] writing this tutorial for someone who posted a comment on Basic Actionscript 3 Game Part 1 called sstng so if you would like to know how to do a certain thing in a language then feel free to [...]

Leave a Reply