Home > Advanced ActionScript 3 > Creating a small shooting game

Creating a small shooting game



In this tutorial you will learn how to create a small “shooting stars” game using Flash and ActionScript 3. Let’s get started!

1. Open a new Flash document and save it under “ShootingGame.fla”. Set its size to 500 x 300 pixels.

2. First of all we need to draw the sight of our weapon. So rename the first layer to “sight”. Take the Oval Tool and from the properties panel set its properties to Solid, Disabled Fill, Stroke height should be set to 3.
Then take the Selection tool, select a part of the circle and cut it like this:

The final step is to take the Line tool and to draw a cross. Just change the Stroke height value to 1.5. Select the whole sight and convert it to Movie Clip with instance name “sight” and registration point center.

3. Now our sight is ready and we need to draw a star. Take the PolyStar Tool and set it properties like this:

If your star is ready, select it and convert it to Movie Clip with registration point center. Linkage the star to class Star by going to the Library panel (Ctrl + L), right-clicking the Star movie clip and then setting the class name. Remove the star from the stage.

4. Take the text tool and first create a Static text field and type “Score:”. Then create a dynamic text field with instance name “points_txt” and position it next to the static text field. Embed the Numeral values for the font and for color choose #FF0000. Align the text fields to the bottom right part of the stage.

5. Create a new layer called “actions”, open Actions Panel and type this:


//importing tween classes
import fl.transitions.easing.*;
import fl.transitions.Tween;

//hiding the cursor
Mouse.hide();

//creating a new Star instance
var star:Star = new Star();
//creating the timer
var timer:Timer = new Timer(1000);
//we create variables for random X and Y positions
var randomX:Number;
var randomY:Number;
//variable for the alpha tween effect
var tween:Tween;
//we check if a star instance is already added to the stage
var starAdded:Boolean = false;
//we count the points
var points:int = 0;

//adding event handler on mouse move
stage.addEventListener(MouseEvent.MOUSE_MOVE, cursorMoveHandler);
//adding event handler to the timer
timer.addEventListener(TimerEvent.TIMER, timerHandler);
//starting the timer
timer.start();

function cursorMoveHandler(e:Event):void{
	//sight position matches the mouse position
	sight.x = mouseX;
	sight.y = mouseY;
}

function timerHandler(e:TimerEvent):void{
	//first we need to remove the star from the stage if already added
	if(starAdded){
		removeChild(star);
	}

	//positioning the star on a random position
	randomX = Math.random()*500;
	randomY = Math.random()*300;
	star.x = randomX;
	star.y = randomY;
	//adding the star to the stage
	addChild(star);
	//changing our boolean value to true
	starAdded = true;
	//adding a mouse click handler to the star
	star.addEventListener(MouseEvent.CLICK, clickHandler);
	//animating the star's appearance
	tween = new Tween(star, "alpha", Strong.easeOut, 0, 1, 3, true);
}

function clickHandler(e:Event):void{
	//when we click/shoot a star we increment the points
	points ++;
	//showing the result in the text field
	points_txt.text = points.toString();
}

Here is the final result:

Liked this tutorial? Share and Enjoy:

  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Reddit
  • blogmarks
  • StumbleUpon
  • Technorati
  • TwitThis
  • Yahoo! Buzz
  • NewsVine
  • Slashdot
  1. ros
    December 2nd, 2009 at 19:48 | #1

    @admin I love this code. Really good fun. I added in sounds and it really gave it a kick.
    BUT
    i tried to add another level where the symbol changes and the level gets faster.
    The faster and changing symbol is easy. I put the same but amended code into another frame BUT the previous symbol still flashes up and doesnt stop. How could i recreate the same game in a differnt frame but with differnt character.
    Help would b great.

  2. Nicola
    December 14th, 2009 at 22:56 | #2

    Thanks man, first tutorial is AS3. Works perfect.

  3. Manan Shah
    December 22nd, 2009 at 02:45 | #3

    It’s really good job!!
    It is very helpful for the beginners!
    Thanks!!

  4. remy
    December 23rd, 2009 at 19:27 | #4

    I’m getting 3 errors…

    1119: Access of possibly undefined property x through a reference with static type class. sight.x = mouseX;
    1119: Access of possibly undefined property y through a reference with static type class. sight.y = mousey;
    1120: Access of undefined property points_txt. points_txt.text = points.toString();

  5. Tim
    December 24th, 2009 at 11:00 | #5

    Someone please help me understand the linkage part cause i keep getting this error:
    **Error** Symbol=Star, layer=Layer 1, frame=1:Line 1: The class or interface ‘Star’ could not be loaded.

  6. zach
    January 11th, 2010 at 20:03 | #6

    I keep getting this
    **Error** Scene=Scene 1, layer=actions, frame=1:Line 9: The class or interface ‘Star’ could not be loaded.
    var star:Star = new Star();

    **Error** Scene=Scene 1, layer=actions, frame=1:Line 11: The class or interface ‘Timer’ could not be loaded.
    var timer:Timer = new Timer(1000);

    **Error** Scene=Scene 1, layer=actions, frame=1:Line 16: The class or interface ‘fl.transitions.Tween’ could not be loaded.
    var tween:Tween;

    **Error** Scene=Scene 1, layer=actions, frame=1:Line 20: The class or interface ‘int’ could not be loaded.
    var points:int = 0;

    **Error** Scene=Scene 1, layer=actions, frame=1:Line 29: The class or interface ‘Event’ could not be loaded.
    function cursorMoveHandler(e:Event):void{

    **Error** Scene=Scene 1, layer=actions, frame=1:Line 35: The class or interface ‘TimerEvent’ could not be loaded.
    function timerHandler(e:TimerEvent):void{

    **Error** Scene=Scene 1, layer=actions, frame=1:Line 53: The class or interface ‘fl.transitions.Tween’ could not be loaded.
    tween = new Tween(star, “alpha”, Strong.easeOut, 0, 1, 3, true);

    **Error** Scene=Scene 1, layer=actions, frame=1:Line 56: The class or interface ‘Event’ could not be loaded.
    function clickHandler(e:Event):void{

    Total ActionScript Errors: 8 Reported Errors: 8

  7. admin
    January 12th, 2010 at 00:17 | #7

    Hello,

    @Tim and @zach,
    you have problems with linking the star to a class called Star.
    I made an update for this step and it is now better explained. Please check step 3. again.
    @remy, you have not given the sight and the text field instance names. Please check steps 2. and 4.

  8. January 25th, 2010 at 14:04 | #8

    I read your flash action script this is very good for flash developer.

    thanks for post
    suresh mishra
    flash developer

  9. February 3rd, 2010 at 15:56 | #9

    Great article, thank you for putting all of this information together.

  10. February 3rd, 2010 at 16:06 | #10

    I’m getting:

    Can you help me, please? Using Adobe Flash CS4.

  11. February 3rd, 2010 at 16:07 | #11

    Errors did not come with last post for some reason. Posting again.

    Scene 1, Layer ‘actions’, Frame 1 1120: Access of undefined property sight sight.x = mouseX
    Scene 1, Layer ‘actions’, Frame 1 1120: Access of undefined property sight sight.y = mouseY

  12. February 3rd, 2010 at 16:31 | #12

    Error solved. Misunderstood the “instance name”.

  13. February 17th, 2010 at 16:49 | #13

    Great Tutorial! Do you know of any more AS3 game tutorial sites?

  14. February 24th, 2010 at 06:36 | #14

    Great tutorial! :)

Comment pages
1 2 3 371
  1. November 17th, 2009 at 00:31 | #1