Home > Advanced ActionScript 3 > Creating a small shooting game in AS3

Creating a small shooting game in AS3




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. Tau
    July 28th, 2010 at 12:42 | #1

    How would you add more than one star onto the game?

  2. July 30th, 2010 at 15:59 | #2

    works

  3. BlumenDk
    August 2nd, 2010 at 01:41 | #3

    where do i get Actionscript 3?

  4. female 1
    August 13th, 2010 at 16:39 | #4

    how can i stop this after a certain amount of time for example one minute or two?

  5. jenson
    September 23rd, 2010 at 18:07 | #5

    thanks for teach me…..

  6. phill
    November 11th, 2010 at 07:28 | #6

    How can I add a counter to end game if player hits a designated number of stars and bring up a congrats screen

  7. anymous
    December 17th, 2010 at 15:11 | #7

    Hi Mr. Bussani shoot them stars:)

  8. max
    January 29th, 2011 at 16:06 | #8

    great tutorial really useful since there aren’t many of them unless you get a book but it says “1046: Type was not found or was not a compile-time constant: Star || var star:Star = new Star();” and i have checked all the pieces of code to do with the star and they all capital “s”s and the star movie clip is the right name could you help me please?

  9. admin
    February 2nd, 2011 at 22:59 | #9

    I think you haven’t done the linkage part. Link the movie clip to a Star class.

  10. Alex
    March 27th, 2011 at 18:24 | #10

    You don’t say anything about the ” public class Srtrong extends Object ”
    … this is inside the flash transitions class ?
    ( we automatic import this part of the code ? )
    … I find this part of code with decompilator program !

  11. April 26th, 2011 at 13:30 | #11

    Thank you for sharing us this code. I really need this for my upcoming project. It really helped me a lot and others.))))))

  12. Naushad
    May 1st, 2011 at 00:40 | #12

    How to pause the game and add sound

  13. May 16th, 2011 at 18:12 | #13

    how do you move the star to the movie clip and setting the class name

  14. Anonymous
    May 16th, 2011 at 18:13 | #14

    @Simon Stoevring
    wow you don’t know how to do that

  15. Anonymous
    May 16th, 2011 at 18:23 | #15

    bob smith :works

    hello can you help me with something

  16. Clara
    May 18th, 2011 at 10:29 | #16

    Hi, I have tried doing the shooting program, but the target always behind the star. May I know the reason why?

  17. Anonymous
    July 11th, 2011 at 09:26 | #17

    @bob smith
    how do you increase the stars as the point increases..?

  18. anonymous-programmer
    November 22nd, 2011 at 01:41 | #18

    hi, i know this has been posted almost 2 years ago but i require help with the following.

    basically ive developed this into my flash cs5 as3 presentation, however im trying to develop it so that i am able to use the cursor outside the game (to click on navigations buttons), which its not allowing me to do at the moment.

    any help on this will be much appreciated

  19. anonymous-programmer
    November 22nd, 2011 at 01:54 | #19

    @anonymous-programmer

    ok if any of you guys are having the same problem, i basically removed the following:

    //Mouse.show();
    //stage.addEventListener(MouseEvent.MOUSE_MOVE, cursorMoveHandler);
    //function cursorMoveHandler(e:Event):void{
    //sight position matches the mouse position
    //sight.x = mouseX;
    //sight.y = mouseY;
    //}

    This removes the sight. and allows access to the parent layers

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