# Adding/Removing Listeners

**URL:** <https://forum.kirupa.com/t/adding-removing-listeners/255575>\
**Category:** flash\
**Created:** [March 24, 2008, 10:38pm UTC](https://forum.kirupa.com/t/adding-removing-listeners/255575 "2008-03-24T22:38:16Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![trundrumbalind](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/trundrumbalind/32/696_2.png) [@trundrumbalind](https://forum.kirupa.com/u/trundrumbalind)\
**Post date:** [March 24, 2008, 10:38pm UTC](https://forum.kirupa.com/t/adding-removing-listeners/255575/1 "2008-03-24T22:38:16Z")

</div>

Hi guys, ok I have movieclips which have timeline animations on them as they are animated buttons. So, I want to set up the following listeners…

MOUSE\_OVER -\> Play the movieclip and delete the enterframe when it hits the last frame  
MOUSE\_OUT -\> Rewind the movieclip  
CLICK -\> Delete the listener so basically the button is “selected” and the MOUSE\_OUT won’t happen when the user takes their mouse off the button.

Now it’s the CLICK handler I’m having trouble with. I’m not sure if I quite understand removing listeners yet! Anyways, here’s my code. The current status is that the MOUSE\_OUT action still happens even after the button has been clicked…

```auto
package com {
    import flash.events.Event;
    import flash.events.MouseEvent;
    
    public class MovieClipButton {
        public function MovieClipButton(target) {
            target.addEventListener(MouseEvent.MOUSE_OVER, eventHandler);
            target.addEventListener(MouseEvent.MOUSE_OUT, eventHandler);
            target.addEventListener(MouseEvent.CLICK, eventHandler);
        }
        
        public function eventHandler(e:MouseEvent) {
            switch (e.type) {
                case "mouseOver" :
                    trace("OVER");
                    e.target.addEventListener(Event.ENTER_FRAME, doPlay);
                    break;
                case "mouseOut" :
                    trace("OUT");
                    e.target.addEventListener(Event.ENTER_FRAME, doRewind);
                    break;
                case "click" :
                    trace("CLICK");
                    e.target.removeEventListener(MouseEvent.CLICK, eventHandler);
                    break;
            }
        }
            
        public function doPlay(e:Event) {
            e.target.gotoAndStop(e.target.currentFrame+1);
            if (e.target.currentFrame == e.target.totalFrames) {
                e.target.removeEventListener(Event.ENTER_FRAME, doPlay);
            }
        }
        
        public function doRewind(e:Event) {
            e.target.removeEventListener(Event.ENTER_FRAME, doPlay);
            
            e.target.gotoAndStop(e.target.currentFrame-1);
            if (e.target.currentFrame == 1) {
                e.target.removeEventListener(Event.ENTER_FRAME, doRewind);
            }
        }
    }
}

```

I really hope someone can help. Thanks guys!

PS: One other thing… is there a way to trace out all the listeners (and maybe some data about them, type etc) currently on an object?
