Flash / AS

Silverlight

WPF

ASP.net / PHP

Photoshop

Forums

Blog

About

 


FlashComponents
  Galleries
  Slideshows
  Menus
  Design & Effects
  Audio & Video
  User Interface
  Templates

  

 

 

 

 

 
Advanced
Collision - Page 2
        by Jeremy Moseley : 24 July 2004

In the previous page you learned how to create the animation. This page will explain why all of the code you pasted works!

Explanation
Let's look at this code:

hit = false; //set hit variable to false
for(i in _root.shape1.perimeter) { //loop through all opjects in perimeter clip

The first line is simply setting the hit variable to false because we don't know whether the clips are touching or not this frame. The second line is a for...in loop. This works by looping through all the objects/properties in an array/object. This will loop through all the little rectangles in the perimeter clip and allow us to access them without giving them all separate instance names. Saves time

This chunk is even more fun:

point = new Object(); //create a new generic object to hold the coordinates
point.x = _root.shape1.perimeter[i]._x; //x coordinate
point.y = _root.shape1.perimeter[i]._y; //y coordinate
_root.shape1.localToGlobal(point); //convert coordinates from local (within shape1) to global (_root)
if(this.hitTest(point.x, point.y, true)) { //check for collision between perimeter clip coordinates and shape 2
hit = true; //make our hit variable true
}

This code creates a generic Object called point, and assigns it two properties x and y. These are given the _x and _y values of the current perimeter_clip object respectively. We then use this object on the next line in the globalToLocal method. This method takes a local set of coordinates stored in an object (point in this case) and converts them to global coordinates (coordinates on the root). This function makes changes directly to the object so we can reuse it in the hitTest function.

After we convert the coordinates we can use them to check for a collision using the hitTest function (see Kirupa's Tutorial) that makes our hit variable true. We have the third parameter in the hitTest method set to true so it only tests the actual shape of shape 2 and not the bounding box. This is very important.

After that comes the simple if statement checking the hit variable to see if the clips are touching or not. We do actions depending on the result. Here is the source code:

Download ZIP

So that's it. I hope this tutorial helped you out a little bit. If you have ANY problems always remember you can post at the KirupaForums with any and all your questions. Thanx for reading.

Cheers!

Jeremy Moseley
My Forum

 


page 2 of 2

 

 




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.