Collision Detection With Actionscript

Written by:  • Edited by: Linda Richter
Updated Nov 6, 2009
• Related Guides: Collision Detection

In part 5 we added some enemies and gave the player the ability to fire it weapons. In part 6 we will add collision detection to allow the player to actually shoot the enemies.

Collision detection is the ability to detect when two objects have collided, and then react appropriately. In part 5 we gave the player the ability to fire weapons at the oncoming enemies. The only problem was that these bullets just passed right through the enemies. In part 6 we will add the necessary code to implement collection detection, which will allow us to shoot the enemies down.

Collision detection is one of those seemingly simple concepts that can be incredibly difficult to implement. You will find entire books devoted to the topic of testing for intersections between 2D and 3D shapes. Fortunately for us our collision detection will be quite simple. Each object on the screen will have a rectangular area in which collisions will be detected (the "collision area"). For simplicity this area will be the same as the size of the underlying graphic that is used to display the sprite on the screen. Once these rectangles overlap we will detect a collision.

In order to get the best results these graphics should cropped as closely to the image that is to be displayed as possible. Below is an example of two images that could be used in the game. Both will display exactly the same way because the space around the plane is transparent. However the top image is more optimized for the collision detection system though because it is closely cropped to the actual image of the plane. The bottom image would appear to collide with other objects before it should because the collision detection system doesn't take any notice of the transparent border, and assumes that the entire area of the image will be used as the collision area.

So lets take a look at the changes that are needed in the GameObject class to implement collision detection.

Showing page 1 of 6

Comments

Showing all 4 comments
 
Chompy Nov 2, 2010 5:05 PM
Re: Where is the Dictionary
yakcamydna:
The Dictionary will be imported with the import flash.utils.*; line that was added to the top of the GameObjectManager.as (isn't bolded). I also noticed that where the levelEnd variable is reset in Level.startup() isn't bold (which causes the game to end every 2 seconds in any game played after the first player/enemy collsion).
If you are just skimming the code to add in as you follow along like I am you may have missed both of those additions. Hope that helps!
yakcamydna Jul 15, 2010 10:29 AM
where is the Dictionary?
I'm kinda stuck with compiling this one. I keep getting error message:
FlexFighters5/src/GameObjectManager.as(27): col: 30 Error: Type was not found or was not a compile-time constant: Dictionary.

protected var collisionMap:Dictionary = new Dictionary();

I have downloaded the source and can't find a Dictionary class anywhere.

I'm very new to programming so probably missed something glaring somewhere.

The thing is in the previous section I managed to add an additional class Nasty and was able to make them fly alongside the Enemy in red planes.

I'm hoping to give this new class a different collision behavior (different sound on explosion say) and as far as I understand what you have written one of the first steps would be to add them to the dictionary like below

Key: "Player" Value: {"Enemy", "EnemyWeapon", "Powerup", "Nasty" }

although I am now beginning to think that this list may be a virtual thing inside the programme.

are you able to shed any light on why I can't compile? and if your able say whether i'm heading in the right direction for giving my new Nasty class a different collision behaviour.

many thanks, andy
Matthew Casperson Jan 18, 2010 4:57 PM
RE: Collision Detection With Actionscript
The Collision Detection Kit at http://coreyoneil.com/portfolio/index.php?project=5 may have what you are looking for.
Scott Jan 18, 2010 9:37 AM
Per Pixel Collision Detection
As much as i think that these tutorials set out a great understanding of actionscript and flex i do have a few questions surrounding collision detection.

The way you demonstrate here is perfect for this genre of game, but what about when a more accurate system is needed. The bitmapdata class has a hittest function that allows for per pixel collisions. However if a matrix transformation is applied to it then this is no longer the case. Can you give some tutorials helping out with collision detection types and matrix transformations please?
 
blog comments powered by Disqus
Email to a friend