對象相交檢測樣本

來源:互聯網
上載者:User

在flex編程中我們有時需要判斷兩個對象是否相交使用obj1.hitTestObject(obj2)方法。

下面給出一個具體的樣本。

 

代碼

<?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>

 

 測試點與對象是否相交。

myCanvas.hitTestPoint(x,y,true)

myCanvas目標對象。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.