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

  • Home
  • SEARCH
  • 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 5999599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:34:31+00:00 2026-05-23T00:34:31+00:00

I have this code: for each(var tool in tools){ tool.addEventListener(MouseEvent.MOUSE_DOWN, function(){ trace(tool); //Always the

  • 0

I have this code:

for each(var tool in tools){
tool.addEventListener(MouseEvent.MOUSE_DOWN, function(){
    trace(tool); //Always the last tool  
 });
}

How do I bind the value of tool to the function so that it’s accessible on callback?

  • 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-05-23T00:34:32+00:00Added an answer on May 23, 2026 at 12:34 am

    You have to use a function inside a function to properly bind the scope. It’s kind of a hack in AS3. It’s better not to go down that rabbit hole if you can help it. If you must, though…

    for(var tool:Tool in _tools){
      var getHandler(scope:Tool):Function{
        var t:Tool = scope;
        return function(e:MouseEvent):void{trace(t)}
      }
      tool.addEventListener(MouseEvent.CLICK, getHandler(tool));
    }
    

    EDIT: And of course, any variables you need to work with in the handler should be passed into getHandler as well… so instead of just accepting the scope param, you’d also pass your id, count, current state, or whatever.

    EDIT2: But ask yourself this question. How will you remove that event listener? That’s the biggest reason I say to avoid this rabbit hole entirely. It’s possible, but it’s usually more trouble than using a more OOP way of solving this problem than lambda functions.

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

Sidebar

Related Questions

I have this js code: $(#startSearch).live(click, function(event) { $(input:checkbox[name='searchId']:checked).each(function() { var searchId = $(this).val();
I have this code: $.getJSON(Featured/getEvents, function(data){ $.each(data.events, function(i,event){ var title = event.title.substr(0,20); $(#title-+i).text(Text); if
so I have this code: $('input.myinput').each(function(){ var id = $(this).val(); var myajax = function(){
I have this following code: $(.preview).find(#panel).each(function(i, val) { var x = + $(val).html(); $(x).find(.dmPageHeadline).prepend(
I have this piece of code: this.json.each(function(obj, index) { var li = new Element('li');
I have this code function calculateTotal() { var total = 0; $(.quantity).each(function() { if
I have this code function getSelectData(id) { jQuery(id).change(function () { var value=''; jQuery(id+ option:selected).each(function
i have this code: $(.link).each(function() { group += 1; text += 1; var links
I have this code: var myRSS =[]; function rssReader() { $.getJSON('bbc.php', function(data){ console.log(data); $.each(data[0].channel.item,
I have this code in a js file: function buildRolePicker() { var pnl =

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.