Shooting game part 1
Objectives
Give you an understanding of events Flash
Build on your knowledge of variables
TUTORIAL Part 1
Open plane.fla
Use the drawing tools to create a custom cursor that will be used in the game, it should look like below.

Convert the graphic to a movie clip and give it a name shooterMC. In the library right click on the shooterMC and select properties and check the box export for actionscript, give a class name of Cursor (just click ok when warning message appaers).

Delete the cursor from the stage.
Select frame 1 of the actions layer and open the actions panel and type the following:
var cursor:MovieClip;
This will just create a variable with the data type cursor which will be used for our custom cursor. Underneath this type the following code:
function initialiseGame():void{
cursor = new Cursor();
addChild(cursor);
cursor.enabled = false;
Mouse.hide();
stage.addEventListener(MouseEvent.MOUSE_MOVE, changeLocation);
}
initialiseGame();
The first line creates a new Cursor from the movie clip we created. It is then added to the stage using addChild. The next line disables any mouse interaction and then the mouse is hidden. Next we add an event listener to the stage listening for any mouse move events, if this occurs then the changeLocation function is called. Underneath this fucntion we are going to create another function that will move the new custom cursor add the following:
function changeLocation(event:MouseEvent):void{
cursor.x = this.mouseX;
cursor.y = this.mouseY;
}
Underneath this we have the function that will be executed every timw the mouse moves. The x and y positions of the mouse are assigned to the custom cursor we have attached to the stage.
Control / Test Movie
Save your work as plane1.fla
Part 2 - Adding planes to the stage
Using plane1 the first thing you need to do is export the movie clip planeMC for actionscript giving it the name of Plane. This is what you did for the custm cursor as above.
We need to extend the variables, on the action layer on the main timeline add the following:
var numberOfPlanes:uint;
var planesCreate:Timer;
var container:MovieClip;
numberOfPlanes=6;
var planeSpeed:uint=5;
The first one is used to determine the number of planes on the stage, then there is a timer which will be used to create planes at certain times in the game and then a container to attach movieclips to. We are setting the total number of planes to 6 and the speed they will move to as 5.
Inside the initialise function give the variables the values.
planesCreate = new Timer (1000, numberOfPlanes);
The first line is the numbers of planes that will appear and then the planesCreate will attach a movieclip every 1 second. Then add the following which will be a container for the movie clips to be loaded into:
container = new MovieClip(); addChild(container);
Following this we need to create the event listener for the planesCreate in order to attach them to the stage, add the following
planesCreate.addEventListener(TimerEvent.TIMER, createPlane); planesCreate.start();
Now underneath the changeLocation function add the following new function
function createPlane (event:TimerEvent):void{
var plane:Plane;
plane = new Plane();
plane.x = 0;
plane.y = Math.random() * stage.stageHeight;
container.addChild(plane);
plane.addEventListener(Event.ENTER_FRAME, movePlane);
}
This function will be called every second from the TimerEvent. Here a plane is being attached to the stage X is 0 because we want it to start at the left hand side and Y will be a random number. The final line adds an eventListener to the plane which will be called and this will call the move plane function.
Now add the following function:
function movePlane(evt:Event):void{
if (evt.target.x<stage.stageWidth){
evt.target.x+=planeSpeed;
} else {
evt.target.removeEventListener(Event.ENTER_FRAME, movePlane);
}
}
This checks to see if the plane x location is less than the width of stage, if this is true than the x position is updated by the speed. If the condition is false then the eventListener is removed.
Control / Test movie
Excercise Part 1
Using last weeks notes see if you can create a function that will get a random speed for each of the planes. Hint
function getSpeed():uint{
// code for random number
return planeSpeed;
}
Excercise 2
See if you can figure out how you would add collision detection between the mouse and the planes.
