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 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

Ask A Question

Stats

  • Questions 130k
  • Answers 130k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes. Use the Smart Tabs plugin. This script allows you… May 12, 2026 at 6:01 am
  • Editorial Team
    Editorial Team added an answer Re-writer rules You could set up a re-writer rules so… May 12, 2026 at 6:01 am
  • Editorial Team
    Editorial Team added an answer I believe that id = *i; i++; and id =… May 12, 2026 at 6:01 am

Related Questions

I am currently running into a problem where an element is coming back from
In the code below I am using a recursive CTE(Common Table Expression) in SQL
In the code below I use mpf_add to add the string representation of two
In the code below I'm trying to load some images and put them in
In the code below I tried in two ways to access the parent version

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.