In Flex programming, we sometimes need to determine whether two objects are intersecting using the obj1.hittestobject (obj2) method.
The following is a specific example.
Code
<? XML version = "1.0" encoding = "UTF-8"?>
<Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "absolute" bordercolor = "# ffffff">
<Mx: SCRIPT>
<! [CDATA [
Import MX. Controls .*;
Import MX. Core .*;
Private var lines: uicomponent = new uicomponent ();
Private var lockx: Int = 0;
Private var locky: Int = 0;
Private var ismove: Boolean = false;
Private function draw (): void {
Lines. ID = "line_1 ";
Lines. Graphics. linestyle (2, 0x0099ff, 1 );
Lines. Graphics. moveTo (100,100 );
Lines. Graphics. lineto (300,300 );
Stage. addeventlistener (mouseevent. mouse_down, function (E: mouseevent ){
VaR OBJ: uicomponent=uicomponent(e.tar get );
If(e.tar get is uicomponent & obj. ID = "line_1 ")
{
Lockx = mouseX-lines.x;
Locky = mouseY-lines.y;
Ismove = true;
}
});
Stage. addeventlistener (mouseevent. mouse_move, mousemovehandler );
Stage. addeventlistener (mouseevent. mouse_up, function (E: mouseevent)
{
Ismove = false;
});
Addelement (lines );
}
Private function mousemovehandler (Event: mouseevent): void {
If (ismove)
{
Lines. x = This. mousex-lockx;
Lines. Y = This. mousey-locky;
If (lines. hittestobject (mycanvas ))
{
Lblmiddle. Text = "in ";
}
Else
{
If (lblmiddle. Text = "coming in ")
{
Lblmiddle. Text = "out ";
}
}
}
}
]>
</MX: SCRIPT>
<Mx: canvas id = "mycanvas" x = "200" Y = "200" width = "100" Height = "100" alpha = "0.5" backgroundcolor = "0xff0000"> </MX: canvas>
<Mx: button x = "20" Y = "20" label = "Draw line" Click = "Draw ()"/>
<Mx: label x = "300" Y = "300" width = "400" Height = "200" fontsize = "72" color = "0xff0000" id = "lblmiddle"/>
</MX: Application>
Whether the test point and the object are intersection.
Mycanvas. hittestpoint (X, Y, true)
The target object of mycanvas.