Movie Clip Collision Detection Flash

Have you ever wanted to detect the intersection of two elements in a Flash movie? If two Movie Clip instances overlap on the Stage, how would you know? How would you tell ActionScript to look for an overlap? In Flash 5, there are two primary types of intersections (or collisions):

  • User-dragged collisions
  • Script- or time-based collisions

Using _dropTarget
A collision between two Movie Clip Objects can occur if the user drags one Movie Clip instance to the location of anotherMovie Clip instance. We first examined the startDrag() action and method mentioned earlier. In the dog movie, we used the _dropTarget property of a Movie Clip Object to detect whether the area of one Movie Clip Object occupied the area of another Movie Clip Object. To recap, you can test the intersection of two Movie Clips with the following code:

This code could occur on a Button instance within the first Movie Clip instance. When the user clicks the Button instance, the Movie Clip startDrag method is invoked and the user can drag the Movie Clip instance on the Stage. When the user releases the mouse, the _dropTarget property (which returns target paths in Slashes notation) is evaluated to convert the target path to Dots notation. If the _dropTarget property returns the path to another instance, then the if condition will see whether the path matches _root.mcInstance2. If the paths match, then the trace action indicating an overlap is executed. Otherwise, a separate trace action will notify us that the instance is not on top of mcInstance2.

Collision detection with advanced scripting
You can also perform more advanced collision detection using the hitTest method of the Movie Clip Object. hitTest will do exactly what it says, it will test to see if a “hit” occurred between two elements. hitTest has two formats:

With this method, you can determine whether the X and Y coordinates are within the space occupied by the Movie Clip instance. onClipEvents such as mouseMove can be used to constantly check for a hit occurrence:

This code will report a trace action anytime the mouse pointer is moved within the artwork of the Movie Clip instance to which the onClipEvent action is attached. The shape flag attribute of hitTest defines the actual test area for the hit. If the shape flag is set to true, then a hit only occurs if the X and Y coordinates occur within the actual artwork of the Movie Clip instance. If the shape flag is set to false, then a hit will occur whenever the X and Y coordinates occur within the bounding box of the Movie Clip instance. If the left circle uses a shape flag of true, then a hit is reported whenever the X and Y coordinates occur within the shape of the circle (not within the bounding box) shows below. If the right circle uses a shape of false, then a hit is reported when the X and Y coordinates occur within the bounding box.

The shape flag determines the boundary of the Movie Clip instance for the hitTest method.

The shape flag determines the boundary of the Movie Clip instance for the hitTest method

The other format for the hitTest method is to simply specify a target path to compare for a hit occurrence. With this syntax, you cannot use a shape flag option; if any area of the bounding box for a Movie Clip instance touches the bounding box of the tested instance, then a hit occurs. For example, you can modify the ActionScript used earlier to indicate a hit between instances, instead of X and Y coordinates:

This code assumes that other actions are actually initiating a startDrag action. Also, we have omitted the other half of the if condition in both this example and the previous example. If you omit a condition operator and test condition, then ActionScript assumes that you are testing for a true result (as a Boolean value). The following if conditions are exactly the same:

Therefore, to test for a true value with any if statement, specify the variable (or method) that has a Boolean value. The hitTest method will yield either a true (a hit has occurred) or a false (no hit has occurred) result. Note that, with scripting languages, it is more common to use the former example for testing true conditions.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

Flash Topics