3

我以为这是一个简单的任务,但我错了。

我使用精灵来显示图像,当用户拖动它(MOUSE_DOWN 和 MOUSE_MOVE)时,我得到了位置MOUSE_MOVE并计算了偏移量:

 var current: Point = new Point(event.localX, event.localY);
 sprite.x = current.x - start.x;
 sprite.y = current.y - start.y;

它有效但不流畅。有更好的解决方案吗?


更新

经过一天的调试,终于找到了原因。

更大的fps可以让它更流畅,但这不是这个问题的关键。

关键是我应该stage用来听MOUSE_MOVE,而不是图像本身。在获取鼠标位置时,我应该使用event.stageX/Y(or stage.mouseX/Y),而不是event.localX/Y. 从event.localX/Y运动图像来看,既不稳定也不平滑,这导致了我的问题。

以下是我的工作代码,享受它:)

package {
    import flash.display.Bitmap;
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.geom.Matrix;
    import flash.geom.Point;
    import flash.net.URLRequest;
    import flash.text.TextField;

    public class DragIssue extends Sprite {

        private static const URL: String = "assets/m1.jpg";

        private var self: DragIssue;

        private var sprite: Sprite;

        private var startPoint: Point;

        private var offsetX: Number = 0;
        private var offsetY: Number = 0;

        public function DragIssue() {
            self = this;
            init();
            loadImage();
        }

        protected function init(): void {
            if (stage == null) {
                this.addEventListener(Event.ADDED_TO_STAGE, on_addedToStage);
            } else {
                stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
            }

            function on_addedToStage(event: Event): void {
                self.removeEventListener(Event.ADDED_TO_STAGE, on_addedToStage);
                stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
            }
        }

        public function loadImage(): void {
            var loader: Loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
            loader.load(new URLRequest(URL));

            function imageLoaded(event: Event): void {
                var bitmap: Bitmap = event.target.content as Bitmap;

                self.sprite = new Sprite();
                sprite.addChild(bitmap);

                self.addChild(sprite);

                sprite.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
                sprite.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
            }
        }

        private function onMouseMove(event: MouseEvent): void {
            if (startPoint) {
                sprite.x = offsetX + event.stageX - startPoint.x;
                sprite.y = offsetY + event.stageY - startPoint.y;
            }
        }

        private function onMouseUp(event: MouseEvent): void {
            startPoint = null;
            offsetX = sprite.x;
            offsetY = sprite.y;
        }

        private function onMouseDown(event: MouseEvent): void {
            startPoint = new Point(event.stageX, event.stageY);
        }

    }
}
4

4 回答 4

2

来,试试这个。它只是一个简单的黑色方块,但在你真正开始拖动它之前它看起来还不错。如前所述,将帧速率设置为更高的值是理想的。在这种情况下,出于内存原因,我决定在 MOUSE_DOWN 中将帧速率提高到 60fps,然后在 MOUSE_UP 中将其降低到 24。你显然可以随心所欲地改变它。

import flash.display.*;
import flash.events.*;


var startX:Number;
var startY:Number;
var shape:Sprite = new Sprite();
shape.graphics.beginFill(0x000000);
shape.graphics.drawRect(0,0,50,50);
shape.graphics.endFill();
this.addChild(shape);

shape.addEventListener(MouseEvent.MOUSE_DOWN,this.mouseDown);

function mouseDown(e:MouseEvent = null):void{
    stage.frameRate = 60;
    startX = stage.mouseX - shape.x;
    startY = stage.mouseY - shape.y;
    stage.addEventListener(MouseEvent.MOUSE_MOVE,this.mouseMove);
    shape.addEventListener(MouseEvent.MOUSE_UP,this.mouseUp);
}

function mouseMove(e:MouseEvent = null):void{
    shape.x = stage.mouseX - startX;
    shape.y = stage.mouseY - startY;
}

function mouseUp(e:MouseEvent = null):void{
    shape.removeEventListener(MouseEvent.MOUSE_UP,this.mouseUp);
    stage.removeEventListener(MouseEvent.MOUSE_MOVE,this.mouseMove);
    stage.frameRate = 24;
}

确保您正在删除 MOUSE_UP 上的 MOUSE_MOVE 事件。那是关键。否则,您在每个 MOUSE_DOWN 上重新添加事件并最终同时重复运行相同的代码。抱歉,我的语法不是 100% 正确的;我在 CS5.5 中非常快速地将其组合在一起,而不是在 Flash Builder 中进行。

于 2012-09-07T21:29:45.317 回答
1

如果您正在寻找流畅的拖动体验,可以使用以下方法:(假设您拖动的东西称为“dragee”,此代码在 Dragee 的父级范围内)

function startDragging():void {
    dragee.addEventListener(Event.ENTER_FRAME,dragUpdate);
}

function stopDragging():void {
    dragee.removeEventListener(Event.ENTER_FRAME,dragUpdate);
}

var decay:Number = .25; //1 is no decay, .1 would be cazy slow

function dragUpdate(e:Event):void {
    dragee.x += decay * (mouseX - dragee.x);
    dragee.y += decay * (mouseY - dragee.y);
}
于 2012-09-07T17:04:51.373 回答
1

您必须使用高帧率才能平滑移动。最佳帧率是 60fps,因为它是大多数 LCD 显示器的默认值。

我手头没有代码,所以我把代码放在这里(未经测试)

yourClip.addeventListenner(MouseEvent.MOUSE_DOWN, startDrag);

function startDrag(e:MouseEvent = null):void{
    // record positon of the mouse relative to the clip
    deltaX=stage.mouseX-yourClip.x;
    deltaY=stage.mouseY-yourClip.y;
    // attach on mouse move event
    yourClip.addeventListenner(MouseEvent.MOUSE_MOVE, updateDrag);
    // attach stop event (on Stage)
    Stage.addeventListenner(MouseEvent.MOUSE_UP, stopDrag);
    }
function updateDrag(e:MouseEvent = null):void{
    yourClip.x=stage.mouseX-deltaX
    yourClip.y=stage.mouseY-deltaY
}

function stopDrag(e:MouseEvent = null):void{
   yourClip.removeEventListenner(MouseEvent.MOUSE_MOVE, updateDrag);
   stage.removeEventListenner(MouseEvent.MOUSE_UP, stopDrag);
}
于 2012-09-07T16:13:42.463 回答
0

尝试调用event.updateAfterEvent()您的拖动处理程序。该函数告诉 Flash 立即重绘舞台,而不是等到下一帧。

于 2012-09-08T00:30:35.677 回答