Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7887783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:40:15+00:00 2026-06-03T05:40:15+00:00

I have learned this method of passing values with addEventLister. Here is the code:

  • 0

I have learned this method of passing values with addEventLister. Here is the code:

for (var i:uint = 0; i < asteroids.length; i++)
{

     asteroids[i].x = Math.random() * 450;
     asteroids[i].y = Math.random() * 450;
     asteroids[i].addEventListener(MouseEvent.MOUSE_UP, function(e:MouseEvent){
         changeValue(e, otherArguments);
     });

}



public function changeValue(event:MouseEvent, otherArguments:Object):void
{

    playSound(anote);
    trace(event.currentTarget);

}

but there is no explanation about how to remove the event listener from

  asteroids[i].addEventListener(MouseEvent.MOUSE_UP, function(e:MouseEvent){
     changeValue(e, otherArguments);
 });
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T05:40:16+00:00Added an answer on June 3, 2026 at 5:40 am

    That is because you cannot unhook an anonymous function if you don’t have a reference to it. If you’d like, you can always keep a reference around:

    asteroidsMouseUpHandler = function(e:MouseEvent){
        changeValue(e, otherArguments);
    };
    
    asteroids[i].addEventListener(MouseEvent.MOUSE_UP, asteroidsMouseUpHandler);
    
    asteroids[i].removeEventListener(MouseEvent.MOUSE_UP, asteroidsMouseUpHandler);
    

    Of course, if these things are happening in separate member functions, then you need to define a member variable (var asteroidsMouseUpHandler:Function). At that point, you might just want to create it as a named function instead. You’d also only want to do this once (not in a loop) to avoid clobbering your reference. Really, I am showing this code for illustrative purposes only.

    Thinking about this further, you might be doing this because the otherArguments is specific to the particular asteroids[i] instance so that you are actually creating a new anonymous function every time you hook it. In that case, if that is what you really want to do, you can keep track of your anonymous functions in a dictionary:

    var asteriodsCallbacks:Dictionary = new Dictionary();
    

    Then, you can keep track of the functions there:

    asteroidsCallbacks[asteroids[i]] = asteroidsMouseUpHandler;
    asteroids[i].addEventListener(MouseEvent.MOUSE_UP, asteroidsMouseUpHandler);
    

    And then when you want to unhook, you can look it up:

    asteroids[i].removeEventListener(MouseEvent.MOUSE_UP, asteroidsCallbacks[asteroids[i]]);
    

    Of course, I am ignoring existential checks for simplicity. You’d have to add that as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have always written regexes like this <A HREF=([^]*) TARGET=_blank>([^<]*)</A> but I just learned
This is a follow on question to an earlier one. I have learned some
I have learned that in a class you can declare variables and methods. They
I have learned so much from http://www.summerofnhibernate.com/ nhibernate screen casts that i wonder why
I have learned Perl and PHP.So I want to do simple real time project
I have learned to use Lua a lot of the time and I also
I have learned from the sencha doc how to create a simple MVC application,
I have learned from various tutorial that If a client can reasonably be expected
I have learned that Windows uses UTF-16LE on x86/x64 systems. What about Linux? Which
One CSS rule I have learned is that you should use the relative em

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.