ActionScript - 鼠标控制逻辑问题

时间:2011-02-14 18:28:43

标签: actionscript-3 logic mouseevent

我试图用相同的鼠标事件序列(向下,向上,向上)控制2个不同的形状(红色矩形和蓝色矩形),但一次只能移动一个形状。

形状沿x轴水平锁定。

从舞台左侧开始的红色矩形具有右对齐,而舞台右侧的蓝色矩形具有左对齐。在MOUSE_MOVE事件期间移动的矩形是最接近* MOUSE_MOVE *事件的 stageX 属性的形状。

最近的矩形是通过平均每个矩形的x属性( averageX )与MOUSE_MOVE stageX 属性相关来确定的。因此,如果 stageX 小于 averageX ,则左侧的形状应移动。比 averageX 更重要,右边的形状。

当控件在MOUSE_UP之前的后续MOUSE_MOVE事件期间更改矩形时,会出现问题。在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);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

在注册mouseMoveHandler之前,您应该检查哪个Rect将被移动。然后改变必要矩形的位置。我使用currentRect变量来表示可移动的rect:

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);
        }
    }
}