First time here? First time here? Newsletter Signup Newsletter
651326 Members and growing! | Flash Jobs
Search tutorials
Author: David Doull | Website: http://www.artifactinteractive.com.au

Step 3: Collision detection

We now have one key thing to do to turn this into a game. We need to add in collision detection.

We want to detect when a laser hits an enemy spaceship and consequently make the spaceship explode and disappear. Similarly we want to detect when an enemy spaceship collides with the players spaceship, at which point the game will end.

So how do we detect collisions in flash?

The spaceship, enemies and lasers are all movieclips. So we need a simple way to detect collisions between movieclips.

The answer is hitTest. Flash 5 introduced a method called hitTest which allows us to detect collisions between two movie clips and its really easy to use.

If you have two movieclips with instance names movie1 and movie2 then the code to detect a collision would be movie1.hitTest(movie2).

A few words about hitTest

There are actually two different ways you can use hitTest.

Usage 1: You can use it to detect the collision (or overlap) between two movie clips using the syntax: movie1.hitTest(movie2) This will be true if the bounding boxes of the two movieclips are overlapping.

So what's a bounding box? Think of it as an invisible rectangle drawn around the edge of a movie clip. If you click on a movie clip in flash you will see the bounding box highlighted (see right). hitTest detects when the bounding boxes of two movie clips overlap, not when the graphics overlap.

The image to the left shows the bounding boxes for the spaceship and the enemy. The two bounding boxes are overlapping so if we used hitTest to check for a collision it would be true even though the actual graphics don't overlap.

This isn't really a problem because the spaceship graphics mostly fill the bounding box and because the game moves so fast that players are unlikely to notice this lack of precision.

Usage 2: You can use it to detect a collision between a movie clip and a point. The syntax is movie1.hitTest(x, y, shapeFlag) where x and y are the co-ordinates of the point and shapeFlag is either true or false depending of whether you want to detect a collision between the movie clips graphic (true) or the movie clips bounding box (false). This usage of hitTest can give you a more precise collision detection but its not used much in games because we generally want to detect the collision between two movie clips not a point and a movie clip.

» Level Intermediate

Added: 2001-05-04
Rating: 9 Votes: 517
(10 being the highest)
» Author
No details available.
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!

Comments

  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.
Featured Flash FLA
» Author: work2survive
» Title: Girl Walking 1kb
» Description: Walking girl, animation just takes 1kb, using high heels shoes, and fashion model attitude.
Featured Sound Loops
Image for equalize

Audio Player

» Author: BoxCat
» Title: CPU Talk
» Description: From the cyberpunk RPG, "Nameless: the Hackers" Completely Free to use! Keywords: edm hip hop rap electronic mysterious mission hacking chase exciting spy ios ambient chilling love happy sad angry suspense
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: nathan
» Description:

A recorded sound of the Dimensional Fork Gate.