View comments | RSS feed

MovieClip.hitTest()

Availability

Flash Player 5.

Usage

my_mc.hitTest(x:Number, y:Number, shapeFlag:Boolean) : Boolean
my_mc.hitTest(target:Object) : Boolean

Parameters

x The x coordinate of the hit area on the Stage.

y The y coordinate of the hit area on the Stage.

The x and y coordinates are defined in the global coordinate space.

target The target path of the hit area that may intersect or overlap with the instance specified by my_mc. The target parameter usually represents a button or text-entry field.

shapeFlag A Boolean value specifying whether to evaluate the entire shape of the specified instance (true), or just the bounding box (false). This parameter can be specified only if the hit area is identified using x and y coordinate parameters.

Returns

A Boolean value of true if my_mc overlaps with the specified hit area, false otherwise.

Description

Method; evaluates the instance specified by my_mc to see if it overlaps or intersects with the hit area identified by the target or x and y coordinate parameters.

Usage 1: Compares the x and y coordinates to the shape or bounding box of the specified instance, according to the shapeFlag setting. If shapeFlag is set to true, only the area actually occupied by the instance on the Stage is evaluated, and if x and y overlap at any point, a value of true is returned. This is useful for determining if the movie clip is within a specified hit or hotspot area.

Usage 2: Evaluates the bounding boxes of the target and specified instance, and returns true if they overlap or intersect at any point.

Example

The following example uses hitTest() to determine if the movie clip circle_mc overlaps or intersects the movie clip square_mc when the user releases the mouse button:

square_mc.onPress = function() {
   this.startDrag();
};
square_mc.onRelease = function() {
   this.stopDrag();
   if (this.hitTest(circle_mc)) {
      trace("you hit the circle");
   }
};

See also

MovieClip.getBounds(), MovieClip.globalToLocal(), MovieClip.localToGlobal() 

Comments


helentriolo said on Aug 27, 2005 at 11:39 AM :
Why is the target parameter described like this on this page? "The target path of the hit area that may intersect or overlap with the instance specified by my_mc. The target parameter usually represents a button or text-entry field." When I've used this form of hitTest, target is another movieclip and never a 'button or text-entry field' -- can you provide an example of what you mean?
No screen name said on Sep 2, 2005 at 5:51 AM :
It would be nice to see an example of hitTest() used with the x, y and shapeflag parameters.
swartz1999 said on Oct 20, 2005 at 11:51 PM :
Hi, helentriolo. You are correct: you can specify a movie clip as the target parameter. The example provided illustrates this.

Here's an example that shows how to specify a text field as a target:

createEmptyMovieClip("square_mc", getNextHighestDepth());
createTextField("sampleText", getNextHighestDepth(), 150, 75, 100, 100);
sampleText.text = "Sample text"
square_mc.beginFill(0x006090);
square_mc.moveTo(50, 50);
square_mc.lineTo(50, 100);
square_mc.lineTo(100, 100);
square_mc.lineTo(100, 50);
square_mc.lineTo(50, 50);

square_mc.onPress = function() {
this.startDrag();
};
square_mc.onRelease = function() {
this.stopDrag();
if (this.hitTest(sampleText)) {
trace("Hit.");
}
};
swartz1999 said on Oct 20, 2005 at 11:57 PM :
Here is the signature of the first usage described:

public hitTest(x:Number, y:Number, [shapeFlag:Boolean]):Boolean

The second usage takes only one parameter: the target object.

Here is an example that shows the first usage (with three parameters). It uses the hitTest() method to determine if the triangle_mc movie clip overlaps the point (100, 75) (about 50 pixels to the right of the center of the initial position of the triangle) when the user releases the mouse button:

createEmptyMovieClip("triangle_mc", getNextHighestDepth());
triangle_mc.beginFill(0x006090);
triangle_mc.moveTo(50, 50);
triangle_mc.lineTo(100, 150);
triangle_mc.lineTo(0, 150);
triangle_mc.lineTo(50, 50);

var hit_X = 100;
var hit_Y = 75;
var shapeFlag = true;

triangle_mc.onPress = function() {
this.startDrag();
};

triangle_mc.onRelease = function() {
this.stopDrag();
if (this.hitTest(hit_X, hit_Y, shapeFlag)) {
trace("Hit.");
}
};

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/mx2004/main_7_2/00001516.html