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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:37:37+00:00 2026-05-11T00:37:37+00:00

In the code below I’m trying to load some images and put them in

  • 0

In the code below I’m trying to load some images and put them in the stage as soon as they get individually loaded. But it is bugged since only the last image is displayed. I suspect it’s a closure problem. How can I fix it? Isn’t the behaviour of closures in AS3 the same as in Java Script ?

var imageList:Array = new Array(); imageList.push({'src':'image1.jpg'}); imageList.push({'src':'image2.jpg'}); var imagePanel:MovieClip = new MovieClip(); this.addChildAt(imagePanel, 0);  for (var i in imageList) {     var imageData = imageList[i];     imageData.loader = new Loader();      imageData.loader.contentLoaderInfo.addEventListener(         Event.COMPLETE,          function() {             imagePanel.addChild(imageData.loader.content as Bitmap);             trace('Completed: ' + imageData.src);                      });      trace('Starting: ' + imageData.src);     imageData.loader.load(new URLRequest(imageData.src));    } 
  • 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. 2026-05-11T00:37:38+00:00Added an answer on May 11, 2026 at 12:37 am

    Isn’t the behaviour of closures in AS3 the same as in Java Script ?

    Yes, JavaScript does exactly the same thing. As does Python. And others.

    Although you define ‘var imageData’ inside the ‘for’, for loops do not introduce a new scope in these languages; in fact the variable imageData is bound in the containing scope (the outer function, or in this case it appears to be global scope). You can verify this by looking at imageData after the loop has completed executing, and finding the last element of imageList in it.

    So there is only one imageData variable, not one for each iteration of the loop. When COMPLETE fires, it enters the closure and reads whatever value imageData has now, not at the time the function was defined(*). Typically the for-loop will have finished by the point COMPLETE fires and imageData will be holding that last element from the final iteration.

    (* – there exist ‘early-binding’ languages that will evaluate the variable’s value at the point you define a closure. But ActionScript is not one of them.)

    Possible solutions tend to involve using an outer function to introduce a new scope. For example:

    function makeCallback(imageData) { return function() {     imagePanel.addChild(imageData.loader.content as Bitmap);     trace('Completed: ' + imageData.src);                                                                                                      } } ... imageData.loader.contentLoaderInfo.addEventListener(Event.COMPLETE, makeCallback(imageData)); 

    You /can/ put this inline, but the doubly-nested function() starts to get harder to read.

    See also Function.bind() for a general-purpose partial function application feature you could use to achive this. It’s likely to be part of future JavaScript/ActionScript versions, and can be added to the language through prototyping in the meantime.

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

Sidebar

Related Questions

The code below get image from some location - and make this image compress
Code below works great with aspx view engine, i am trying to convert it
The code below enables a control (a label for instance) to show drag images
The code below is an example of what I am trying to do, I
The code below extracts links from a web page and shows them in a
Code below does not run correctly and throws InvalidOperationExcepiton . public void Foo() {
Code below is not working as expected to detect if it is in design
Code below is working well as long as I have class ClassSameAssembly in same
The code below shows a sample that I've used recently to explain the different
The code below gives me this mysterious error, and i cannot fathom it. I

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.