The good old hitTest " method has been removed, or better, it"s been split into 2 separate methods called hitTestObject and hitTestPoint.
The use of these methods isn"t much different from the old hitTest.
Let"s see how they work"
The Document Class I"ve created is this:
Code:
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
public class ProvaNuovoHitTest extends MovieClip
{
public function ProvaNuovoHitTest()
{
primoCaso();
secondoCaso();
}
private function primoCaso():void
{
black_mc.addEventListener(Event.ENTER_FRAME,controllaMouse);
}
private function controllaMouse(e:Event):void
{
if(black_mc.hitTestPoint(mouseX,mouseY,true))
debug_0_txt.text='si';
else
debug_0_txt.text='no';
}
private function secondoCaso():void
{
circle_1_mc.buttonMode=true;
circle_1_mc.addEventListener(MouseEvent.MOUSE_DOWN,sposta);
circle_1_mc.addEventListener(MouseEvent.MOUSE_UP,ferma);
}
private function sposta(m:MouseEvent):void
{
circle_1_mc.startDrag(true);
circle_1_mc.addEventListener(Event.ENTER_FRAME,controllaContatto);
}
private function controllaContatto(e:Event):void
{
if(circle_1_mc.hitTestObject(circle_0_mc))
debug_1_txt.text='si';
else
debug_1_txt.text='no';
}
private function ferma(m:MouseEvent):void
{
circle_1_mc.stopDrag();
circle_1_mc.removeEventListener(Event.ENTER_FRAME,controllaContatto);
}
}
}
Here is the result:
Let"s analyze the code:
In the primoCaso ( method ) the script checks if the clip hits the mouse with the event ENTER_FRAME.
In this case I use the method hitTestPoint with its 3 parameters:
-x of the point
-y of the point
-forma: it"s a Boolean value ( true/false ) with which you can decide if you want to apply hitTestPoint only to the real shape of the object or to the containing frame.
I suggest you try to change that value to better understand the difference J
In the secondoCaso ( method ) I control if the red MovieClip ( moveable ) hits the blue one using the hitTestObject method with its only parameter ( the actual object with which we"re checking the collision ).
With the hitTestObject method, you can"t choose to apply it to the containing frame. In order to do so, advanced trigonometry algorithms and complicated Actionscript is required ( we"ll see it later ).
Later !
Bookmarks