【问题标题】:How do you display a sprite location in real time during drag and drop (AS3)如何在拖放过程中实时显示精灵位置(AS3)
【发布时间】:2019-12-20 18:17:29
【问题描述】:

我试图弄清楚如何在对象被拖过舞台时显示其 X/Y 坐标。

如果我有一个位于 0,0 的正方形并将其拖动到新位置 - 例如 50,50 - 我想显示正方形的位置 它正在被拖动,不只是在它掉落的时候。所以坐标数会随着物体的拖动而不断变化。

现在我的代码只在拖动开始和拖动停止时检测对象的 X/Y 位置:

import flash.events.MouseEvent;

this.addEventListener(MouseEvent.MOUSE_DOWN, startDragging, true);
this.addEventListener(MouseEvent.MOUSE_UP, stopDragging, true);

function startDragging(e:MouseEvent) {

square1.startDrag();
xDisplay_txt.text = square1.x;
yDisplay_txt.text = square1.y;

}

function stopDragging(e:MouseEvent) {

testStage1.stopDrag();
xDisplay_txt.text = testStage1.x;
yDisplay_txt.text = testStage1.y;

}

感谢任何帮助。谢谢。

【问题讨论】:

    标签: actionscript-3 drag-and-drop dynamic-text


    【解决方案1】:

    您需要在拖动该东西时定期调用某个处理程序以更新输出文本。最简单的方法是使用 ENTER_FRAME 事件,正如其名称所述,每一帧都会触发。

    import flash.display.Sprite;
    
    import flash.events.Event;
    import flash.events.MouseEvent;
    
    // We may drag different objects, we need to know which one.
    var currentDrag:Sprite;
    
    // A list of objects we can drag.
    var aList:Array = [square1, square2];
    
    // Iterate over all the items in the list
    // and subscribe to each one separately.
    for each (anItem:Sprite in aList)
    {
        anItem.addEventListener(MouseEvent.MOUSE_DOWN, onStart);
    }
    
    function onStart(e:MouseEvent):void
    {
        // Store, which one object is being dragged.
        // Read on difference between Event.target and Event.currentTarget.
        currentDrag = e.currentTarget as Sprite;
        currentDrag.startDrag();
    
        // Subscribe to ENTER_FRAME event to control the way of things.
        // We need to do it only if we drag square1, as requested.
        if (currentDrag == square1)
        {
            addEventListener(Event.ENTER_FRAME, onFrame);
        }
    
        // Subscribe to stage, because this way you will handle the
        // MOUSE_UP event even if you release the mouse somewhere outside.
        stage.addEventListener(MouseEvent.MOUSE_UP, onStop);
    }
    
    function onFrame(e:Event):void
    {
        // This event fires every frame, basically, every 40 ms.
        // Round the coordinates and update the texts.
        xDisplay_txt.text = int(currentDrag.x).toString();
        yDisplay_txt.text = int(currentDrag.y).toString();
    }
    
    function onStop(e:MouseEvent):void
    {
        // That is also why we are keeping the refference
        // to the object we are dragging: to know which one to drop.
        currentDrag.stopDrag();
    
        // We're not dragging anything anymore.
        currentDrag = null;
    
        // Unsubscribe, as it is no longer needed.
        // That's fine even if we didn't subscribed to in in the first place.
        removeEventListener(Event.ENTER_FRAME, onFrame);
    
        // Unsubscribe from this one too.
        stage.removeEventListener(MouseEvent.MOUSE_UP, onStop);
    }
    

    【讨论】:

    • 有机物 - 谢谢!!这完美地工作。这会受到舞台上其他可拖动对象的影响(或影响)吗?
    • @dzinermachine 上面的代码只跟踪一个可通过实例名称square1访问的对象。您是否有要拖动和跟踪的类似对象的列表?
    • 不,我只需要跟踪 1 个对象 - square1 的 X/Y 坐标。但是,我在舞台上有两个可以拖放的对象:square1 和 square2。拖动 square1 时,动态显示坐标,隐藏 square2。拖动 square1 时一切正常。但是当我拖动 square2 时,它仍然会像拖动 square1 一样被隐藏。
    • 我知道这是一个单独的问题,所以也许我应该开始另一个问题线程哈哈......
    • @dzinermachine 最直接的方法是为 square2 定义一组单独的 onStart 和 onStop 处理程序。但就可伸缩性和脚本大小而言,最直接的方式往往是最差的。我会在一分钟内更新我的帖子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-04
    • 2013-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多