2012-12-16 21 views
1

因此,我正在以編程方式向主時間線添加動畫片段對象。 即使在那些使用計時器事件的影片剪輯中,它們一旦添加到舞臺上就會移動。從主時間線中刪除來自動畫片段的事件偵聽器

因此,當我的遊戲結束時,我想從舞臺上刪除movieclip對象,我可以做到這一點,但它會導致錯誤,因爲對象的偵聽器仍然存在。

我無法從主時間線訪問movieclip對象時間軸中的代碼以刪除監聽器,我不知道如何從movieclip時間線中刪除監聽器,當它們被刪除時!

我希望這是有道理的!

幫助!

回答

0

當從舞臺上移除DisplayObject時,有事件可以接收通知。只需爲Event.ADDED,Event.ADDED_TO_STAGE,Event.REMOVED和Event.REMOVE_FROM_STAGE設置偵聽器,即可爲影片剪輯實現實時循環。添加動畫片段時開始播放內容。刪除監聽器,停止計時器等。

+0

嗨謝謝你,我會給它一個去,非常感謝! – ZiggyBird

1

您的MovieClip可以偵聽Event.REMOVED_FROM_STAGE以編程方式刪除先前添加的所有事件偵聽器。這裏有一些示例代碼來展示它是如何完成的。

// add this code to an ActionScript class that extends Sprite 
    // call initClips() in the constructor 

    private var _clipA:MovieClip; 
    private var _clipB:MovieClip; 
    private var _clipACount:int; 
    private var _clipBCount:int; 

    private function initClips():void 
    { 
     // create 2 movieclips and add some common event listeners 
     _clipA = new MovieClip(); 
     _clipA.addEventListener(Event.ADDED_TO_STAGE, onClipAddedToStage); 
     _clipA.addEventListener(Event.REMOVED_FROM_STAGE, onClipRemovedFromStage); 
     _clipA.addEventListener(Event.ENTER_FRAME, onEnterFrameClipA); 


     _clipB = new MovieClip(); 
     _clipB.addEventListener(Event.ADDED_TO_STAGE, onClipAddedToStage); 
     _clipB.addEventListener(Event.REMOVED_FROM_STAGE, onClipRemovedFromStage); 
     _clipB.addEventListener(Event.ENTER_FRAME, onEnterFrameClipB); 

     _clipACount = 0; 
     _clipBCount = 0; 

     addChild(_clipA); 
     addChild(_clipB); 
    } 



    private function onClipAddedToStage(event:Event):void 
    { 
     var clip:MovieClip = event.currentTarget as MovieClip; 
     if(clip == _clipA) 
     { 
      // position movieclip to dummy values 
      clip.x = 40; 
      clip.y = 40; 
     } 
     else if(clip == _clipB) 
     { 
      clip.x = 200; 
      clip.y = 100; 
     } 
    } 

    private function onClipRemovedFromStage(event:Event):void 
    { 
     var clip:MovieClip = event.currentTarget as MovieClip; 
     // good habit to null check in case "as" casting fails 
     if(clip != null) 
     { 
      // remove event listeners for this movieclip instance 
      clip.removeEventListener(Event.ADDED_TO_STAGE, onClipAddedToStage); 
      clip.removeEventListener(Event.REMOVED_FROM_STAGE, onClipRemovedFromStage); 

      // there is no penalty for trying to remove event listeners that were not added to this object instance 
      clip.removeEventListener(Event.ENTER_FRAME, onEnterFrameClipA); 
      clip.removeEventListener(Event.ENTER_FRAME, onEnterFrameClipB); 
     } 
    } 


    private function onEnterFrameClipA(event:Event):void 
    { 
     _clipACount++; 
     trace("onEnterFrame for clipA " + _clipACount); 

     if(_clipACount > 10) 
     { 
      // for demonstration purposes 
      // remove self after a few frames pass 
      removeChild(_clipA); 
     } 

    } 

    private function onEnterFrameClipB(event:Event):void 
    { 
     _clipBCount++; 
     trace("onEnterFrame for clipB " + _clipBCount); 

     if(_clipBCount > 20) 
     { 
      removeChild(_clipB); 
     } 
    }