Flash-一次拖动两个动画片段吗?

时间:2020-03-06 14:27:18  来源:igfitidea点击:

我正在尝试创建与此类似的地图应用程序。单击图像左侧的SWF预览选项卡。具体来说,请注意我们如何平移,地图上的可单击按钮也随之移动。基本上,他们是怎么做到的?

我的应用程序有一个地图,我们可以使用startDrag()函数单击并在其中平移。我有一个单独的图层,上面有其他可单击的影片剪辑,我想跟随地图图层的平移。不幸的是,Flash限制我们一次只能拖动一个影片剪辑。有人提出了使用原型的解决方案,但是我无法使其正常工作,而且我不确定是否是因为我没有使用ActionScript 3.0。

有人能为我概述一种更好的方法来完成我正在尝试做的事情,还是一种更好的方法来完成我目前正在做的事情吗?欣赏它。

解决方案

我无法拖动我们链接的任何int SWF文件,只能通过mouseWheel进行缩放。

我们可以使用事件mousedown,mouseMove和mouseUp一次拖动多个movieClip。

通过Mouse.addListener(object)添加事件的事件处理程序。

  • 在mouseDown中设置一些标志并记住当前的鼠标位置。
  • 在mouseMove中,如果设置了此标志,则计算移动偏移量,只需更改要拖动的所有动画片段的.x和.y。
  • 在mouseUp中将标志设置为false

我们可以将动画片段添加到父剪辑并让事件处理程序拖动父剪辑吗?

简单的。将地图和可单击的按钮放入新的MovieClip中,可以将其命名为InteractiveMapContainer或者类似名称,然后在InteractiveMapContainer上调用startDrag方法,并且将其拖动后仍然可以单击按钮。

雅库布·科特拉(Jakub Kotrla)的方法也将很好地工作,尽管它稍微复杂一些。

我认为defmeta的方法是最好的方法,如果有可能使所有内容成为"主要"可拖动对象的子对象-它是最快的(就UI响应时间而言)并且最简单,并且所需的代码量最少。

如果这不是一个选择,那么我们可以使用其他两种方法:

  • 如果我们使用的是Flex,则可以通过侦听其上的MoveEvent.MOVE事件并相应地移动自己,来使其他对象与地图一起移动
  • 我们可以自己实现拖动功能,以便对其进行更多控制(请参见下文)。

这是一个为对象实现拖动功能的示例,该对象还将随其移动一堆"同级"对象。在这个例子中

  • 任何以下划线开头的变量都是包含此代码的类的私有成员
  • _siblings是一个字典,其中的键是需要移动的其他对象。

private var _dragStartCoordinates:Point = null;
private var _siblingsDragStartCoordinates:Dictionary = null;

private function mouseDownHandler(event:MouseEvent):void
{
    this.startDrag();
    _dragStartCoordinates = new Point(this.x, this.y);

    _siblingsDragStartCoordinates = new Dictionary(true);
    for (var sibling:DisplayObject in _siblings)
    {
        _siblingsDragStartCoordinates[sibling] = new Point(sibling.x, sibling.y);
    }

    stage.addEventListener(MouseEvent.MOUSE_UP, dragMouseUpHandler, false, 0, true);
    stage.addEventListener(MouseEvent.MOUSE_MOVE, dragMouseMoveHandler, false, 0, true);
}

private function dragMouseUpHandler(event:MouseEvent):void
{
    stage.removeEventListener(MouseEvent.MOUSE_UP, dragMouseUpHandler, false);
    stage.removeEventListener(MouseEvent.MOUSE_MOVE, dragMouseMoveHandler, false);
    this.stopDrag();
    moveSiblings();
    _dragStartCoordinates = null;
    _siblingsDragStartCoordinates = null;
}

private function dragMouseMoveHandler(event:MouseEvent):void
{
    moveSiblings();
}

// expects _dragStartCoordinates and _siblingsDragStartCoordinates
// to be set
private function moveSiblings():void
{
    var xDiff:Number = this.x - _dragStartCoordinates.x;
    var yDiff:Number = this.y - _dragStartCoordinates.y;

    for (var sibling:DisplayObject in _siblings)
    {
        sibling.x = _siblingsDragStartCoordinates[sibling].x + xDiff;
        sibling.y = _siblingsDragStartCoordinates[sibling].y + yDiff;
    }
}

感谢hasseg提供的代码,节省了一些时间。这是我对公众使用的看法...

package Tools
{
    import flash.display.MovieClip; 
    import flash.events.*; 
    import flash.geom.Rectangle; 
    import flash.utils.setInterval; 
    import flash.utils.clearInterval; 
    import flash.display.MovieClip;
    import flash.display.DisplayObject;
    import flash.display.Sprite;
    import flash.geom.Point;
    import flash.utils.Dictionary;

    public class DragSync { 
        private var _dragStartCoordinates:Point = null;
        private var _siblingsDragStartCoordinates:Dictionary = null;
        private var _primaryItem:Object = null;
        private var _dragWithPrimary:Array = null;

        public function DragSync(primaryItem:Object,dragWithPrimary:Array)
        {
            _primaryItem = primaryItem;
            _dragWithPrimary = dragWithPrimary;

            _primaryItem.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
            _primaryItem.addEventListener(MouseEvent.MOUSE_UP, dragMouseUpHandler);
            _primaryItem.addEventListener(MouseEvent.MOUSE_MOVE, dragMouseMoveHandler);
        }

        private function mouseDownHandler(event:MouseEvent):void
        {
            _dragStartCoordinates = new Point(_primaryItem.x, _primaryItem.y);
            _siblingsDragStartCoordinates = new Dictionary(true);

            for each (var sibling:DisplayObject in _dragWithPrimary)
            {
                _siblingsDragStartCoordinates[sibling] = new Point(sibling.x, sibling.y)
            }
        }

        private function dragMouseUpHandler(event:MouseEvent):void
        {
            moveSiblings();
            _dragStartCoordinates = null;
            _siblingsDragStartCoordinates = null;
        }

        private function dragMouseMoveHandler(event:MouseEvent):void
        {
            moveSiblings();
        }

        // expects _dragStartCoordinates and _siblingsDragStartCoordinates
        // to be set
        private function moveSiblings():void
        {
            if (!_dragStartCoordinates || !_siblingsDragStartCoordinates) return;

            var xDiff:Number = _primaryItem.x - _dragStartCoordinates.x;
            var yDiff:Number = _primaryItem.y - _dragStartCoordinates.y;

            for each (var sibling:DisplayObject in _dragWithPrimary)
            {
                sibling.x = _siblingsDragStartCoordinates[sibling].x + xDiff;
                sibling.y = _siblingsDragStartCoordinates[sibling].y + yDiff;
            }
        }

    } 
}

非常感谢你。我将其更改如下:
包裹
{
导入flash.display.MovieClip;
导入flash.events。*;
导入flash.geom.Rectangle;
导入flash.utils.setInterval;
导入flash.utils.clearInterval;
导入flash.display.MovieClip;
导入flash.display.DisplayObject;
导入flash.display.Sprite;
导入flash.geom.Point;
导入flash.utils.Dictionary;

public class DragSync { 
    private var _dragStartCoordinates:Point = null;
    private var _siblingsDragStartCoordinates:Dictionary = null;
    private var _primaryItem:Object = null;

私人var _workSpace:Object = null;
私人var _dragWithPrimary:Array = null;

public function DragSync(primaryItem:Object,workSpace:Object, dragWithPrimary:Array)
    {
            _primaryItem = primaryItem;
            _dragWithPrimary = dragWithPrimary;
_workSpace = workSpace;

            _workSpace.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
            _workSpace.addEventListener(MouseEvent.MOUSE_UP, dragMouseUpHandler);
            _workSpace.addEventListener(MouseEvent.MOUSE_MOVE, dragMouseMoveHandler);
_workSpace.addEventListener(MouseEvent.MOUSE_OUT, dragOut);
    }

    private function mouseDownHandler(event:MouseEvent):void
    {
            trace("event mouse down");
_primaryItem.startDrag();
_dragStartCoordinates = new Point(_primaryItem.x, _primaryItem.y);
            _siblingsDragStartCoordinates = new Dictionary(true);

            for each (var sibling:DisplayObject in _dragWithPrimary)
            {
                    _siblingsDragStartCoordinates[sibling] = new Point(sibling.x, sibling.y)
            }
    }

    private function dragMouseUpHandler(event:MouseEvent):void
    {
            moveSiblings();
_primaryItem.stopDrag();
            _dragStartCoordinates = null;
            _siblingsDragStartCoordinates = null;
    }

    private function dragMouseMoveHandler(event:MouseEvent):void
    {
            trace("event mouse MOVE MOVE");
moveSiblings();
    }

私有函数dragOut(event:MouseEvent):void
{
_primaryItem.stopDrag();
}

// expects _dragStartCoordinates and _siblingsDragStartCoordinates
    // to be set
    private function moveSiblings():void
    {
            if (!_dragStartCoordinates || !_siblingsDragStartCoordinates) return;

            var xDiff:Number = _primaryItem.x - _dragStartCoordinates.x;
            var yDiff:Number = _primaryItem.y - _dragStartCoordinates.y;

            for each (var sibling:DisplayObject in _dragWithPrimary)
            {
                    sibling.x = _siblingsDragStartCoordinates[sibling].x + xDiff;
                    sibling.y = _siblingsDragStartCoordinates[sibling].y + yDiff;
            }

    }

}

}