2011-02-14 75 views
0

我試圖控制2個不同的形狀(紅色矩形和藍色矩形)與鼠標事件(向下,移動,向上)的相同序列,但只有一個形狀在一個時間必須移動。的ActionScript - 鼠標控制邏輯問題

形狀被沿着x軸水平鎖定。

紅色矩形,它開始時在舞臺上的左側,具有右側登記而在舞臺上的右側的藍色矩形具有左註冊。在MOUSE_MOVE事件期間移動的矩形是最接近* MOUSE_MOVE *事件的stageX屬性的形狀。

最接近矩形由每個矩形(averageX)的相對於x屬性平均到MOUSE_MOVE stageX屬性確定。因此,如果stageX小於averageX,左側的形狀應該移動。刨花比averageX,形狀在右邊。

問題時控制在隨後的事件MOUSE_MOVE改變矩形,MOUSE_UP之前發生。在MOUSE_DOWN期間設置的變量適用於首先移動的矩形,但當另一個矩形移動時,事情變得危險。

基本上,其X屬性是最接近MOUSE_MOVE事件的stageX屬性應該沿着向左或向右鼠標事件,一次一個移動矩形。

爲用下面的代碼示例簡單起見,總是可以某處兩個矩形之間發生一個事件MOUSE_DOWN。

package 
{ 
import flash.display.Sprite; 
import flash.display.Shape; 
import flash.events.MouseEvent; 

//Class 
public class RectsTest extends Sprite 
    { 
    //Variables 
    private var redRect:Shape; 
    private var blueRect:Shape; 

    private var mouseDownOrigin:Number; 
    private var redRectOrigin:Number; 
    private var blueRectOrigin:Number; 
    private var averageX:Number; 

    //Constructor 
    public function RectsTest() 
     { 
     init(); 
     } 

    //Initialize 
    private function init():void 
     { 
     redRect = createRect(0xFF0000, -200, 200, 100); 
     redRect.x = 200; 
     redRect.y = 200; 

     blueRect = createRect(0x0000FF, 0, 200, 100); 
     blueRect.x = stage.stageWidth - 200; 
     blueRect.y = 300; 

     addChild(redRect); 
     addChild(blueRect); 

     stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownEventHandler); 
     } 

    //Create Rect 
    private function createRect(color:Number, x:Number, width:Number, height:Number):Shape 
     { 
     var result:Shape = new Shape(); 
     result.graphics.beginFill(color); 
     result.graphics.drawRect(x, 0, width, height); 
     result.graphics.endFill(); 

     return result; 
     } 

    //Mouse Down Event Handler 
    private function mouseDownEventHandler(evt:MouseEvent):void 
     { 
     mouseDownOrigin = evt.stageX; 
     redRectOrigin = redRect.x; 
     blueRectOrigin = blueRect.x; 

     averageX = (redRect.x + blueRect.x)/2; 

     stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpEventHandler); 
     stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveEventHandler); 
     } 

    //Mouse Move Event Handler 
    private function mouseMoveEventHandler(evt:MouseEvent):void 
     { 
     if (evt.stageX < averageX) 
      redRect.x = redRectOrigin + evt.stageX - mouseDownOrigin; 

     if (evt.stageX > averageX) 
      blueRect.x = blueRectOrigin + evt.stageX - mouseDownOrigin; 

     averageX = (redRect.x + blueRect.x)/2; 
     } 

    //Mouse Up Event Handler 
    private function mouseUpEventHandler(evt:MouseEvent):void 
     { 
     stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpEventHandler); 
     stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveEventHandler); 
     } 
    } 
} 

回答

0

您應該檢查在註冊mouseMoveHandler之前將移動哪個Rect。然後改變必要矩形的位置。我使用currentRect變量來表示可移動矩形:

package 
{ 
import flash.display.Sprite; 
import flash.display.Shape; 
import flash.events.MouseEvent; 

//Class 
public class RectsTest extends Sprite 
    { 
    //Variables 
    private var redRect:Shape; 
    private var blueRect:Shape; 

    private var mouseDownOrigin:Number; 
    private var redRectOrigin:Number; 
    private var blueRectOrigin:Number; 
    private var averageX:Number; 

    //Constructor 
    public function RectsTest() 
     { 
     init(); 
     } 

    //Initialize 
    private function init():void 
     { 
     redRect = createRect(0xFF0000, -200, 200, 100); 
     redRect.x = 200; 
     redRect.y = 200; 

     blueRect = createRect(0x0000FF, 0, 200, 100); 
     blueRect.x = stage.stageWidth - 200; 
     blueRect.y = 300; 

     addChild(redRect); 
     addChild(blueRect); 

     stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownEventHandler); 
     } 

    //Create Rect 
    private function createRect(color:Number, x:Number, width:Number, height:Number):Shape 
     { 
     var result:Shape = new Shape(); 
     result.graphics.beginFill(color); 
     result.graphics.drawRect(x, 0, width, height); 
     result.graphics.endFill(); 

     return result; 
     } 

    //Mouse Down Event Handler 
    private function mouseDownEventHandler(evt:MouseEvent):void 
     { 
     mouseDownOrigin = evt.stageX; 
     redRectOrigin = redRect.x; 
     blueRectOrigin = blueRect.x; 

     averageX = (redRect.x + blueRect.x)/2; 

     stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpEventHandler); 
     stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveEventHandler); 
     } 

    //Mouse Move Event Handler 
    private function mouseMoveEventHandler(evt:MouseEvent):void 
     { 
     if (evt.stageX < averageX) { 
      redRect.x = redRectOrigin + evt.stageX - mouseDownOrigin; 
      redRectOrigin = redRect.x; 
     } 


     if (evt.stageX > averageX) { 
      blueRect.x = blueRectOrigin + evt.stageX - mouseDownOrigin; 
      blueRectOrigin = blueRect.x; 
     } 

     mouseDownOrigin = evt.stageX; 
     averageX = (redRect.x + blueRect.x)/2; 
     } 

    //Mouse Up Event Handler 
    private function mouseUpEventHandler(evt:MouseEvent):void 
     { 
     stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpEventHandler); 
     stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveEventHandler); 
     } 
    } 
} 
+0

我覺得你誤會了,因爲你的代碼不起作用。拖動時應該能夠改變移動的形狀。所以如果我將紅色矩形拖到屏幕的右側,一旦stageX接近藍色矩形,紅色矩形將停止移動,而藍色矩形開始移動 - 全部在鼠標移動之前。 – TheDarkIn1978 2011-02-14 20:03:14