A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Problem removing EventListener

  1. #1
    flashguy
    Join Date
    May 2005
    Location
    In the mountains
    Posts
    341

    Problem removing EventListener

    Hi,

    I am struggling with this. I have an EventListener function that receives a parameter. For any reason, seems that Flash doesn't like it when is time to REMOVE it:

    Code:
    var counter:Number = 0;
    
    btnCreate.addEventListener(MouseEvent.CLICK,Create);
    btnRemove.addEventListener(MouseEvent.CLICK,Remove);
    
    function Create(e:MouseEvent):void
    {
    	addEventListener(Event.ENTER_FRAME,MyEvent(""));
    }
    
    function Remove(e:MouseEvent):void
    {
            // the following line of code doesn't remove the Event!
    	removeEventListener(Event.ENTER_FRAME,MyEvent(""));
    }
    
    function MyEvent(myparameter:String):Function
    {
    	return function (e:Event):void
    	{
    		counter++;
    		trace (counter);
    	}
    }
    What is the trick to remove an EventListener when it does have a parameter?

    Thanks!
    Visit my business at http://www.ballooncreator.com - Software Tool For Party Balloons Online Design!

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    The result of MyEvent("") is a function which takes an event. That is a valid event listener function. But when you run MyEvent("") again, you get a NEW identical function. That function was never added as a listener, so it cannot be removed as a listener. You would have to save a reference to the original result of MyEvent in order to remove it.

    You may want to "memoize" the MyEvent function to cache results for inputs so that you get out the exact same function each time you put in the same input. Note however, that that will prevent these result listener functions from being garbage collected.

    My FunctionUtils class contains a memoize function. http://cosmodro.me/blog/2008/aug/26/...ity-functions/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center