Results 1 to 5 of 5

Thread: how does contentLoaderInfo work

  1. #1

    how does contentLoaderInfo work

    I'm trying to figure out how contentLoaderInfo works; I'm trying to display 5 images on stage and place them horizontally based on their individual widths. Everything works fine except inside displayImage i remains at 5. Each width traces out correctly, but i stays at 5. What is it about contentLoaderInfo that I'm not understanding?

    for (var i:uint= 0; i<5; i++)
    {
    loader = new Loader();
    var urlReq:String = "images/Photos/imageSm_000" + i + ".jpg";
    loader.load(new URLRequest(urlReq));


    loader.contentLoaderInfo.addEventListener(Event.CO MPLETE, displayImage;

    function displayImage(e:Event):void
    {
    trace(e.target.content.width);
    trace(i);
    addChild(e.target.content);
    e.target.content.x = 40 + i * e.target.content.width;
    }
    }

    Thanks

  2. #2
    238
    posts
    Registered User
    every time you declare the function displayImage, you overwrite the existing copy...so after i has iterated 5 times, the last overwritten version of displayImage has i set to 5.

    change your addeventlistener to:
    Code:
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, displayImage(i);
    and move function displayImage out of the for loop, and rewrite it like this:
    Code:
    function displayImage(i:uint):Function
                          {
         return function(e:event):void{
    
    trace(e.target.content.width); trace(i); addChild(e.target.content); e.target.content.x = 40 + i * e.target.content.width; }
    }

  3. #3

    The function traces out in an odd way

    Quote Originally Posted by sebrofm View Post
    every time you declare the function displayImage, you overwrite the existing copy...so after i has iterated 5 times, the last overwritten version of displayImage has i set to 5.

    change your addeventlistener to:
    ActionScript Code:
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, displayImage(i);



    and move function displayImage out of the for loop, and rewrite it like this:
    ActionScript Code:
    function displayImage(i:uint):Function
    {
    return function(e:event):void{<blockquote> trace(e.target.content.width);
    trace(i);
    addChild(e.target.content);
    e.target.content.x = 40 + i * e.target.content.width;
    }
    </blockquote>}
    Thanks, sebrofm
    I appreciate the help, but when I run this and trace out (i, e.target.content.width) I get
    4 133
    2 77
    3 65
    1 77
    0 77

    which makes it difficult to place the images predictably. The sequence matches up (that is, the fourth image is 133 pixels, etc) but the order is not useful.

    Thanks again

  4. #4
    In talking to another developer who is just learning as3, his guess is that the order is determined by when loaded was complete, not the order I specified. I thought that's what the contentLoaderInfo did, that is, wait until the first item loaded before proceeding onto the next item.

  5. #5
    it has nothing to do with contentLoaderInfo, but your loop. See:
    http://www.senocular.com/flash/tutor...#loopfunctions

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012