开发者

ActionScript - Mouse Logic Problem

开发者 https://www.devze.com 2023-01-26 19:58 出处:网络
dumb problem.embarrassed that i haven\'t found a solution.i\'m tired. a red square in on the stage.mouse-down + mouse-drag-up will move the red square downward (+y), while mouse-down + mouse-drag-dow

dumb problem. embarrassed that i haven't found a solution. i'm tired.

a red square in on the stage. mouse-down + mouse-drag-up will move the red square downward (+y), while mouse-down + mouse-drag-down will move the red square upward (-y). this opposite motion is desired.

however, during a mouse drag the square must begin moving from it's current y position, regardless of how many mouse drags have changed it's initialized position. currently, the red square will always begin at stage 0, since my mouseDownOrigin variable is incorrect because my brain is asleep.

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

public class Test extends Sprite
    {
    private var sp:Sprite = new Sprite();
    private var mouseDownOrigin:int;

    public function Test()
        {
        sp.graphics.beginFill(0xFF0000);
        sp.graphics.drawRect(0, 0, 100, 100);
        sp.x = sp.y = 200;
        addChild(sp);

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

    private function mouseDownEventHandler(evt:MouseEvent):vo开发者_开发百科id
        {
        mouseDownOrigin = evt.stageY;
        stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveEventHandler);
        stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpEventHandler);
        }

    private function mouseMoveEventHandler(evt:MouseEvent):void
        {
        sp.y = mouseDownOrigin - evt.stageY;
        }

    private function mouseUpEventHandler(evt:MouseEvent):void
        {
        stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveEventHandler);
        stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpEventHandler);
        }
    }
}


You need to record the red square y position on the MouseUp event handler, practically recording the position you leave the square in.

   private var currentPosition:int;

   private function mouseUpEventHandler(evt:MouseEvent):void
   {
     currentPosition = sp.y;

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

   private function mouseMoveEventHandler(evt:MouseEvent):void
   {
       var n:int = event.stageY - mouseDownOrigin ;
       sp.y = currentPosition - n;
   }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号