Flash Game Design  
 
     
Tutorials Articles
     
AddThis Social Bookmark Button  
 
Shooting Objects Flash Tutorial  
Part 3) Add Missile and Asteroid Collision Detection
Step 13.
So now that we've create the animated asteroid, go back to the main timeline and click on the 1st frame of the 'shot' layer, where we'll be building ont he existing code.
 
Step 14.
Previously a moveBullets function had been created, which adds a bullet, when the space bar is pressed. Next we want to add some code for when bullets reached the top of the screen, or if it hits the asteroid. The moveBullets function is shown below, with the eisting code grey out. The code that hasn't been greyed out is new, and will be explained below.

Line 25: Since the current bullet, in the array that's being looped through, is going to be referenced a number of times, it is assigned to the temporary variable 'b'.

Line 26: The top of the screen is 0 on the Y axis. When the whole of the bullet is outside of the screen we want to remove, so an IF statement is used to check to see if the bullet is above the point (-50 Y) at which it should be removed.

Line 28 - 30: If not (else) the bullet's MC is then removed from the stage and it's reference is removed from the array.

Line 32: A variable is used for when we don't want to check if the bullet is hitting the asteroid. This will be when the asteroid has just been hit.

Line 33: b (the current bullet in the loop) is checked to see if it hits ob, which will be the the instance name given to the asteroid later on. It's been named 'ob' because we may want to use different objects, instead of an asteroid, later on.

Line 34: Now that the bullet has hit the asteroid, 'hit' is set to true, to stop it from being checked.

Line 35: If 'b' hits 'ob', the asteroid animation will play, where all four rocks move away.

Line 36 - 37: The bullet's MC is then removed from the stage and it's reference is removed from the array.

 
Step 15.

Line 42: A hit variable to false, to stop and resume collision detection

Line 43: A variable to set the speed of the object (asteroid).

Line 44: A function to create an object, with the argument (o). When the function is called, the name of the MC's identifier ("asteroid") will be entered as the argument like so: createObject("asteroid").

Line 45: The asteroid will then be attached and assigned to the variable 'ob', so that we then reference it indepedent of what the object is. This will be useful for if a different object is used, since we will only have to change it when the function is called.

Line 46: The asteroid needs to appear randomly on the x axis.

 
Step 16.

Line 48: When the object ("asteroid") has reached the end of its exploding animation or when its moved to the bottom of the stage, it needs to be "reset", so a function is created to do this.

Line 49-50 : The object is given a new random X position to start from and placed at -40 on the Y axis.

Line 51-52 : When the asteroid has reached the end of the exploding animation, it's needs to go back to frame 1 so that's whole again. The restart function also needs to be set back to false, so that the resetObject function isn't called again.

Line 53: Hit needs to be set back to false so that checking can resume, of the bullets hitting the asteroid.

 
Step 17.

Line 55: Next a function is created to move the object (asteroid)

Line 56-57: An IF statement is placed to check if the objects Y value is lower than 250 (the bottom of the stage). If is is then the object needs to be moved.

Line 58-59: If it's not (else) then that's means it's reached beyond 250 and needs to be reset.

Line 61-62: We also need to check if the asteroid has finished exploding, in which case the asteroid is reset.

 
Step 18
 

Line 65: Lastly, the object is created

Line 67: And the moveObject function is added to the onEnterFrame function, so that it's checked all the time.

 
 
And that brings us to the end of this Flash tutorial. Download FLA
Part: 1 | 2 | 3
 
   
 
   
 
Site Copyright © 2008 Nick DS. All rights reserved.