Wednesday, 25 June 2008

Collision testing

This Flash tutorial will teach you about basic collusion testing. Collusion testing uses the hitTest() method which determines whether an object has been hit or not. The hit object can then be prompted to perform some sort of event such as: displaying a message, playing a sound, animated scene or even vanish. So this means that the hitTest() method can be used to initiate some event when an object has been overlapped or intersected. This can be useful for testing movie clips within games, applications etc.

For this tutorial I will show a basic example of a moving car object hitting a tree object. The tree object is the “hitTest”. A message will then be displayed when car object hits the tree object.

Collision testing

Step 1

Create a new Flash document.
Import your objects into the stage by selecting File > Import > Import to stage then select your objects and click ok.
You could alternatively create your own object on the stage at this point.

Step 2

Convert each of your objects into symbols by pressing F8. Give your symbols an appropriate name, check movie clip and click ok.

Using the selection tool (V) select each object in turn and give them an appropriate instance name. eg car_mc, and tree_mc

Step 3

Now you need to create the message for when the objects hit.
Select the text tool (t) and type your message on the stage. eg The objects have hit.
Convert your text into a symbol by pressing F8. Give your symbols an appropriate name, check movie clip and click ok. And then give your text object an appropriate instance name. eg txt_mc.

Step 4

On the timeline insert a new layer called “actions” then right click on the first frame and select Actions.

And enter the following code:

txt_mc._visible = false;
function moveMe() {
if (this.hitTest(tree_mc)) {
txt_mc._visible = true;
this._x -=5;
txt_mc._visible = false;
car_mc.onEnterFrame = moveMe;

**This piece of code contains a function which moves the car object 5 pixels to the right continuously until it hits the tree object. A message is displayed when the tree object is hit.

Step 5

Test your movie clip ctrl + enter.

You should now be able to understand basic collusion testing.

UPDATE: new simple hit test in Actionscript 3.0.

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP