Collision Detection

Page 3 of 3
J.D. Andrews
This user is yet to take control of their account and provide a biography. If you are the author of this article, please contact us via support AT actionscript DOT org.
View all articles by J.D. AndrewsSet Variable: "red_x" = GetProperty ("/red",_x)
Set Variable: "red_y" = GetProperty ("/red",_y)
The code contains two key elements, the action and the property. The actions set value of the variables 'red_x' and 'red_y' to be the x and y properties of the movie clip red
*Definition: Variable - A variable is a mathematical quantity or a symbol that represents it.
For those of you who may need a refresher on what _x and _y are, here is a simple x and y axis picture. You most likely remember this from school.

The x axis is the horizontal line and the y axis is the vertical line. The variable "red_x" contains the x coordinates of the instance of the movie clip red. The variable "red_y" contains the y coordinates. The next 2 lines of actionscript do the same for the instance of green. It sets up the x and y coordinates like so.
Set Variable: "green_x" = GetProperty ("/green",_x)
Set Variable: "green_y" = GetProperty ("/green",_y)
The next 4 lines of actionscript get the height and the width of both movie clips, red and green.
Set Variable: "red_h" = GetProperty ("/red",_height)/2
Set Variable: "green_h" = GetProperty ("/green",_height)/2
Set Variable: "red_w" = GetProperty ("/red",_width)/2
Set Variable: "green_w" = GetProperty ("/green",_width)/2
The next 2 lines of actionscript set up 2 variables for distance .
Set Variable: "distance_x" = red_x-green_x
Set Variable: "distance_y" = red_y-green_y
The distance_x = the movieclip red's x axis minus the movie clip green's x axis.
The distance_y = the movie clip red's y axis minus the movie clip green's y axis.
This is a very important part because it is used to calculate the distance between the two.With out it there is no way to check on how close the two object are to each other based on the _x and _y coordinates of both objects.
The next line of actionscript sets up a variable named area . The area is the height and width of each movie clip. This is also very important, because you need to the size of each object in order to get an accurate collision check
Set Variable: "area" = (red_h+red_w)*(green_h+green_w)
The area is red's height and width * (times) green's height and width.
*Definition: If - The If statement is use to check if certain condition are true.
The next line of action script contains all the above information and is the start of the If statement which will check to see if certain conditions are true.
If (distance_x*distance_x+distance_y*distance_y<=area)
If the distance_x * the distance_x + the distance_y * the distance_y is less then or = to the variable "area" then a collision has occured and the text field box named status prints out "Collision Detected. This is used to give you a visual clue that a collision has taken place.
Set Variable: "/:status" = "Collision Detected"
*Definition Else: The Else statement is used to assign an alternate statement if conditions are false.
Else....
Set Variable: "/:status" = "No Collision Detected"
In this case the text field box named status is instructed to print out..."no Collision Detected.
End If
Which ends the If statement. Now that you are done with the Collision checking actionscript, in the movie clip processor Put this action script in the key frame in frame 2.
Go to and Play (1)
And finally go back to the main time line and use Motion tweening and make the movie clips move across the scene and at some point collide with one another. In my example i made the scene 48 frames long and tweened the movie clips across the scene.

Well that's it. If you have any Question Flash Kit has an excellent message board forum called "Action Scripting". Just go there and post you question and one of the moderators like myself or a member will help you.

