16Jan
[AS 3] Simple Hit Test with hitTestPoint Method
No commentsDisplayObject.hitTestPoint(x:Number, y:Number, shapeFlag:Boolean = false)
This method is great for testing intersections with an X,Y point of the stage. Ideally you would use this with small objects to allow for a more realistic hit test.
I used my circle class from earlier.
package {
import flash.display.Sprite;
public class Circle extends Sprite {
private var radius:Number;
private var color:uint;
private var xcircle:Number;
private var ycircle:Number;
public function Circle(radius:Number=20, color:uint=0x336699, xcircle:Number = 10, ycircle:Number = 20) {
this.x = xcircle;
this.y = ycircle;
this.radius = radius;
this.color = color;
draw();
}
public function draw(){
graphics.beginFill(color);
graphics.drawCircle(0, 0, radius);
graphics.endFill();
}
}
}
Now my document class for the hitTestObject method
package {
import flash.display.Sprite;
import flash.events.Event;
public class HitTestPoint extends Sprite {
private var circle:Circle;
private var circle2:Circle;
private var radius:Number;
private var color:uint;
private var xcircle:Number;
private var ycircle:Number;
public function HitTestPoint(){
init()
}
public function init(){
// CIRCLE 1
// draw the first circle and center it on the stage
circle = new Circle(30,0x333333,stage.stageWidth/2, stage.stageHeight/2);
// display the circle
addChild(circle);
circle.startDrag(true);
// CIRCLE 2
circle2 = new Circle(4,0x000000,200,200);
// display the circle
addChild(circle2);
circle2.alpha = 10;
// create the listeners
addEventListener(Event.ENTER_FRAME, testHit);
}
private function testHit(e:Event):void
{
// hit point area is x = 200 and y = 200
if(circle.hitTestPoint(200, 200, false))
trace ("HIT !!!!");
}
}
}
Categories: Actionscript 3, HitTest Methods
Wednesday, January 16th, 2008 at 12:28 pm and is filed under Actionscript 3, HitTest Methods. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.