2011-05-26 90 views
1

我有一個對象,我需要通過點擊和拖動進行旋轉。在一些AS2代碼之後,每次單擊鼠標時都會讓對象旋轉一點,但無法使其與拖動一起工作。Actionscript 3,拖動時旋轉對象

needle.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag_2); 
stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop_2); 

function fl_ClickToDrag_2(event:MouseEvent):void 
{ 

     var angle = Math.atan2(mouseY-needle.y,mouseX-needle.x); 
     // apply rotation to handle by converting angle into degrees 
     needle.rotation = angle*180/Math.PI; 
     // rotate the grip opposite the handle so it won't rotate along with it 
     //this.grip._rotation = -this._rotation; 
} 

function fl_ReleaseToDrop_2(event:MouseEvent):void 
{ 
    needle.stopDrag(); 
} 

回答

2

嗯,我看到的問題是,MOUSE_DOWN事件僅每點擊觸發一次,所以你只運行在處理程序的代碼一次。

可能有比這更好的辦法,但是這是我會考慮這樣做:

編輯的詳細信息:

public class Test extends MovieClip { 
    private var n:Needle; 

    public function Test() { 
     // constructor code 
     n = new Needle(); 

     stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownF,false,0,true); 
     stage.addEventListener(MouseEvent.MOUSE_UP,mouseUpF,false,0,true); 


     n.x = stage.stageWidth/2; //center needle on stage 
     n.y = stage.stageHeight/2; 
     addChild(n); //add needle to stage 
    } 
    public function mouseDownF(e:MouseEvent):void { 
     stage.addEventListener(MouseEvent.MOUSE_MOVE,rotate,false,0,true); 
    } 
    public function rotate(e:MouseEvent):void { 
     var angle:Number = Math.atan2(mouseY - n.y,mouseX - n.x); //get angle in radians (pythagoras) 

     angle = angle * 180/Math.PI -90; //convert to degrees , the 90 is to have it point to the mouse 

     n.rotation = angle; //rotate 
    } 
    public function mouseUpF(e:MouseEvent):void { 
     stage.removeEventListener(MouseEvent.MOUSE_MOVE,rotate); 
    } 
} 

因此,當用戶單擊(mouseDown)它的激活每次鼠標移動時觸發rotate處理程序的事件偵聽器。當用戶放開點擊時,事件偵聽器被銷燬。添加事件偵聽器時的false,0,true);是使其成爲weakly referenced listener,以便它被垃圾收集器收集,並且不會坐在永遠佔用空間的內存中。

+0

嗯,感謝它改進了一點bot不多。針頭MC卡入不同角度,不隨鼠標移動。當我點擊並拖動時,MC會捕捉到一個新的旋轉,如果我再將鼠標移動一些,它會捕捉到一個新的位置。它也只是順時針旋轉。 – RapsFan1981 2011-05-26 22:05:38

+0

這可能與我的MC的輪換有關。該符號指向右側。 – RapsFan1981 2011-05-26 22:15:28

+0

還沒有工作,不跟隨鼠標只能捕捉到一個新的角度。任何人? – RapsFan1981 2011-05-27 14:16:11