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() {
this._x+=5;
if (this.hitTest(tree_mc)) {
txt_mc._visible = true;
this._x -=5;
}
else
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.



0 comments:
Post a Comment