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 810245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:51:10+00:00 2026-05-15T00:51:10+00:00

Im trying to create a simple loop that creates 50 buttons, adds them to

  • 0

Im trying to create a simple loop that creates 50 buttons, adds them to screen and then when a button is pressed, it traces out that number. I can get it to work by doing stuff I consider hacky (such as using the buttons X/Y location to determine its value), but I’d rather just be able to hold a single value in the function.

The code itself is:

for (var a:int = 0; a < 5; a++) {
    for (var b:int = 0; b < 10; b++) {
        var n = (a * 10) + b + 1;
        var btt:SimpleButton = new BasicGameButton();
        btt.x = 20 + b * 50;
        btt.y = 50 + a * 80;
        addChild(btt);
        btt.addEventListener(MouseEvent.CLICK, function f() { trace(n); } );
    }
}

At the moment, whenever a button is pressed, it simply outputs “50”. Is there a way of “freezing” the value of n when the function is created, for that function? (BasicGameButton is just a square button, created in the flash library)

Many thanks.

  • 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-15T00:51:11+00:00Added an answer on May 15, 2026 at 12:51 am

    this is an AS3 flaw comming from the abandoned ECMA-Script draft AS3 is based on … n is scoped to the innermost function body it is declared in (thus as if it were declared before the loop), although declared in the innermost loop body of your code.

    you can do the following:

    var makeFunction:Function = function (n:int):Function {//this can of course also be a method
        return function (event:MouseEvent):void {
             trace(n);
        }
    }
    
    //in the loop body, use it like this:
    btt.addEventListener(MouseEvent.CLICK, makeFunction(n));
    

    As a side note: Haxe does not have this flaw.

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

Sidebar

Related Questions

I'm trying to create a simple loop that adds a random number of stars,
I am trying to create a simple image slideshow that fades in and out
I'm trying to create a simple for loop that will take all the outputs
I'm trying to create a simple ajax load test script that dynamically creates divs,
I'm trying to create simple script that subscribes a user to a company calendar.
I am trying to create a simple about page that uses JQuery click to
I'm trying to create a simple web page that resembles the following: I've got
I'm trying to create a script that will loop through files that have their
I'm trying to create a very simple stand-alone app that converts CATIA .dat files
I can't figure this out. I've create a simple class that returns an array

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.