0

I have array of images i need to loop throw them to get the url and load them by this function loadImage, the function works fine and loads the images put my problem with the Movieclip block when i add the image to it the image get's added to the last Movieclip in the loop ?

var relatedJSON = JSON.decode(e.target.data);
    var relatedStories = relatedJSON.stories;
    if(relatedStories.length > 0){
        for(var j:Number=0;j<relatedStories.length;j++){
            if(relatedStories[j].yahki_thumb){
                var one = j+1;
                var block = related_stories.getChildByName('related_stories_block'+one);
                // load the image
                loadImage({
                    path:relatedStories[j].yahki_thumb,
                    success:function(e:Event,my_loader:Loader)
                    {
                        my_loader.width = this[block].width;
                        my_loader.height = this[block].height;
                        this[block].img.addChild(my_loader);
                    },
                    error:function(e:Event)
                    {
                        // error
                        echo('error to load an image');
                    }
                });
            }
        }
        // replay button event
        //replay
    }else{
        // error
        echo('no related stories');
    }

1 Answer 1

1

Try this within loop:

   var that:* = this;
   (function(block:*):void{
       loadImage({
                path:relatedStories[j].yahki_thumb,
                success:function(e:Event,my_loader:Loader)
                {
                    my_loader.width = that[block].width;
                    my_loader.height = that[block].height;
                    that[block].img.addChild(my_loader);
                },
                error:function(e:Event)
                {
                    // error
                    echo('error to load an image');
                }
            });
   })(block);

As loading is asyncronous, when your success function is called, block value has already related_stories.getChildByName('related_stories_block'+relatedStories.length) value. That is the reason,elements were added to last MovieClip.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.