2011-06-28 189 views
0

我一直在尋找小時,但仍未找到答案來解決我的問題。如何同時拖動兩個影片剪輯(在單獨的圖層上)

我的目標是製作一張地圖,可以在網頁框架內拖動。 找到合適的代碼來移動地圖在有限的框架是不是太容易,但我設法找到(它就像一個魅力):

zoom3.addEventListener(MouseEvent.MOUSE_DOWN, dragArea); 
zoom3.addEventListener(MouseEvent.MOUSE_UP, dropArea); 

function dragArea(e:MouseEvent):void{ 
var bounds:Rectangle = new Rectangle(
          stage.stageWidth - zoom3.width, 
          stage.stageHeight - zoom3.height, 
          zoom3.width - stage.stageWidth, 
          zoom3.height - stage.stageHeight 
         ); 
zoom3.startDrag(false, bounds); 
} 

function dropArea(e:MouseEvent):void{ 
zoom3.stopDrag(); 
} 

現在美中不足的是,我想在地圖與上面的動畫片段同時拖動,其中包含地區,城市等的名稱... 兩者不能在同一層,因爲我需要在它們之間放置一層紋理。

有沒有簡單的方法來連接兩個圖層並使它們一次移動?

希望我很清楚,英語是我的第二語言。 感謝您的閱讀,也許有所幫助。

+0

裹在一個容器影片剪輯並拖動代替層。 –

+0

中間的紋理圖層不應該移動。我不確定這種方法在這種情況下會起作用嗎? – Ben

+0

也許嘗試並掩蓋它 –

回答

2

,您可以:

  • 當你開始拖拽添加一個enterFrame事件偵聽器(刪除它,當你停止拖動)。通過拖動的動畫片段追蹤位置的差異並將其應用於第二個動畫片段。
  • 倍率的setter方法拖動剪輯的x和y,並且或者鏡像它上拖動剪輯或分派所述第二動畫片段偵聽的事件,並更新相應

選項1:

private var m_prevX:Number = 0.0; 
private var m_prevY:Number = 0.0; 

private function dragArea(e:MouseEvent):void 
{ 
    var bounds:Rectangle = ... 

    // hold the current x and y so that we can get the difference 
    this.m_prevX = zoom3.x; 
    this.m_prevY = zoom3.y; 

    // add the enterframe listener 
    zoom3.addEventListener(Event.ENTER_FRAME, this._onEnterFrame); 

    // start dragging 
    zoom3.startDrag(false, bounds); 
} 

private function dropArea(e:MouseEvent):void 
{ 
    zoom3.stopDrag(); 

    // remove the enter frame listener 
    zoom3.removeEventListener(Event.ENTER_FRAME, this._onEnterFrame); 
} 

// called every frame that we're dragging 
private function _onEnterFrame(e:Event):void 
{ 
    // get the difference in movement 
    var diffX:Number = this.m_prevX - zoom3.x; 
    var diffY:Number = this.m_prevY - zoom3.y; 

    // store the current pos 
    this.m_prevX = zoom3.x; 
    this.m_prevY = zoom3.y; 

    // apply the difference to the other clip 
    myOtherClip.x += diffX; 
    myOtherClip.y += diffY; 
} 

**選項「:** 更面向對象方法你並不需要覆蓋x和y,但zoom3需要擴展類是這樣的:

public class DraggableClip extends MovieClip 
{ 
    /******************************************************/ 

    /** 
    * The name of the event that we dispatch when we're being dragged 
    * and our position changes 
    */ 
    public static const DRAG_UPDATE_POS:String = "drag_update_pos"; 

    /******************************************************/ 

    /** 
    * The x difference in position if we're being dragged 
    */ 
    public var dragDiffX:Number = 0.0; 

    /** 
    * The y difference in position if we're being dragged 
    */ 
    public var dragDiffY:Number = 0.0; 

    /******************************************************/ 

    private var m_prevX:Number = 0.0; // our previous x position 
    private var m_prevY:Number = 0.0; // our previous y position 

    /******************************************************/ 

    /** 
    * Start dragging the clip. This is dispatch an event when 
    * our position changes 
    */ 
    override public function startDrag():void 
    { 
     // reset the drag difference positions 
     this.dragDiffX = 0.0; 
     this.dragDiffY = 0.0; 

     // hold our current position 
     this.m_prevX = this.x; 
     this.m_prevY = this.y; 

     // add an event listener to that we can track the difference 
     // you could also set it to listen to MouseEvent.MOUSE_MOVE if 
     // you wanted 
     this.addEventListener(Event.ENTER_FRAME, this._onEnterFrame); 

     // start dragging 
     super.startDrag(); 
    } 

    /** 
    * Stop dragging the clip 
    */ 
    override public function stopDrag():void 
    { 
     // remove the event listener 
     this.removeEventListener(Event.ENTER_FRAME, this._onEnterFrame); 

     // stop draggin 
     super.stopDrag(); 
    } 

    /******************************************************/ 

    // called every frame we're updating 
    private function _onEnterFrame(e:Event):void 
    { 
     // get the drag difference 
     this.dragDiffX = this.m_prevX - this.x; 
     this.dragDiffY = this.m_prevY - this.y; 

     // store the current x and y 
     this.m_prevX = this.x; 
     this.m_prevY = this.y; 

     // if our position has changed, dispatch an event 
     if(this.dragDiffX != 0.0 && this.dragDiffY != 0.0) 
      this.dispatchEvent(new Event(DraggableClip.DRAG_UPDATE_POS)); 
    } 
} 

然後聽的事件,像這樣:

private function _init():void 
{ 
    // do your init stuff 
    zoom3  = new DraggableClip; 
    myOtherClip = new MovieClip; 

    // add the event listener to zoom3 
    zoom3.addEventListener(DraggableClip.DRAG_UPDATE_POS, this._onDragZoom3); 
} 

private function _onDragZoom3(e:Event):void 
{ 
    // zoom3's position has been changed, update myOtherClip 
    myOtherClip.x += zoom3.dragDiffX; 
    myOtherClip.y += zoom3.dragDiffY; 
} 
+0

這聽起來像是正確的做法。不幸的是,我的閃存方式:/ 感謝您的回答:) – Ben

+0

我已經更新了代碼示例的答案,以便您瞭解發生了什麼。 Option1是快速,骯髒和容易。 Option2適用於當你在尋找可重複使用/有點整齊的東西時 – divillysausages

+0

感謝一羣夥伴,這對你很好。 – Ben

相關問題