我正在尝试创建类似于this的地图应用程序。单击图像左侧的SWF预览选项卡。具体来说,注意到你可以如何平移,地图上的可点击按钮随之移动。基本上,他们是如何做到的?
我的应用程序有一个地图,您可以使用startDrag()函数单击并平移。我有一个单独的图层与其他可点击的影片剪辑,我想跟随地图图层的平底锅。不幸的是,Flash限制您只拖动one movie clip at a time。有人提出了使用prototype的解决方案,但我无法正常工作,我不确定是否因为我使用的是ActionScript 3.0。
任何人都可以勾勒出更好的方式来完成我正在尝试做的事情,或者更好的方式来做我目前正在做的事情吗?欣赏它。
答案 0 :(得分:3)
简单。将地图和点击按钮放入新的MovieClip,您可以将其命名为 interactiveMapContainer 或类似内容,然后在 interactiveMapContainer 上调用startDrag方法,拖动它后,你仍然可以点击按钮。
Jakub Kotrla的方法也会很好用,虽然它稍微复杂一些。
答案 1 :(得分:2)
我认为defmeta的方法是最好的方法,如果它可以让所有东西成为“主要”可拖动对象的孩子 - 它是最快的(就UI响应时间而言)并且最简单,并且需要最少量的代码
如果这不是一个选项,您可以使用其他几种方法:
这是一个为对象实现拖动功能的示例,该对象也会移动一堆“兄弟”对象。在这个例子中,
_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;
}
}
答案 2 :(得分:1)
我无法拖动您链接的SWF中的任何内容,只需通过mouseWheel进行缩放。
您可以使用事件mousedown,mouseMove和mouseUp一次拖动多个movieClip。
通过Mouse.addListener(object)为事件添加事件处理程序。
答案 3 :(得分:1)
感谢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;
}
}
}
}
答案 4 :(得分:1)
非常感谢你。我已将其更改如下: 包 { 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 _workSpace:Object = null; private 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;
}
}
}
}
答案 5 :(得分:0)
可以将动画片段添加到父片段并让事件处理程序拖动父片段吗?