在flex编程中我们有时需要判断两个对象是否相交使用obj1.hitTestObject(obj2)方法。
下面给出一个具体的示例。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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.target);
if(e.target 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="进来了";
}
else
{
if(lblMiddle.text=="进来了")
{
lblMiddle.text="出去了";
}
}
}
}
]]>
</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="画线" click="Draw()"/>
<mx:Label x="300" y="300" width="400" height="200" fontSize="72" color="0xFF0000" id="lblMiddle"/>
</mx:Application>
<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.target);
if(e.target 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="进来了";
}
else
{
if(lblMiddle.text=="进来了")
{
lblMiddle.text="出去了";
}
}
}
}
]]>
</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="画线" click="Draw()"/>
<mx:Label x="300" y="300" width="400" height="200" fontSize="72" color="0xFF0000" id="lblMiddle"/>
</mx:Application>
测试点与对象是否相交。
myCanvas.hitTestPoint(x,y,true)
myCanvas目标对象。