1. Computing

ActionScripting Basics: Collision Test Using hitTest

By

When you select the if statement in Normal Mode, a field will appear above the display area that asks you to define the condition that the if statement is checking for. Enter the following:

this.hitTest(_root.target)

If you’ll remember, this separated by a period is used to tell a function to act upon the object to which the script is attached—so we just told Flash to use the hitTest function to determine if the object defined by “this” (instance name projectile) has entered the area of the bounding box of whatever object is inside the parentheses after hitTest. The basic formula is clipname.hitTest(target). The way that this works with our if statement is that if the check determines the target hasn’t been hit, then the if statement’s condition hasn’t been met, so nothing happens—but if the target has been hit, then hitTest returns a value of true to the if statement. With its conditions met, then the actions specified inside the if statement will happen.

_root tells Flash to make sure to go back up to the very top level of the movie to look for the movie clip referenced by that instance name. This isn’t always necessary, but because Flash works in tiers as you go from the top level into movie clip after movie clip, it’s always best to specify which level you’re acting upon so that Flash knows where to look.

  1. About.com
  2. Computing
  3. Animation
  4. Flash Animation Tutorials
  5. ActionScripting Basics: Collision Test Using hitTest

©2014 About.com. All rights reserved.