A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: Using onLoadInit to store to an Array

  1. #1
    Junior Member
    Join Date
    Jan 2008
    Posts
    24

    Using onLoadInit to store to an Array

    I'm loading images through an XML, using MovieClipLoader, and then placing the images in a movie clip one after another using onLoadInit (So they are all on the stage, in a 'filmstrip' layout). I want to store the widths of the images in an array, so that later, I can recenter the image strip on any particular image based on the combined widths. My problem is that when I try to push the widths into an array within the onLoadInit I get an undefined array. The widths, are there, I can trace them, but the array doesn't work in that command for some reason. Heres the code:

    Code:
    // A FEW OF THE VARS, AND THE MCL WITH LISTENER
    var position = 0;
    var loader = new MovieClipLoader();
    var i = 0;
    var mclListener:Object = new Object();
    loader.addListener(mclListener);
    
    //THE LOOP THAT PULLS THE IMAGES AND PLACES THEM INTO COVER_MC				
    for(i=0; i<imageArray.length; i++){
    	var empty = _mp3Player.cover_mc.createEmptyMovieClip("image"+i,i);
    	
            loader.loadClip(imageArray[i],empty);
    	};
    
    //THE ONLOADINIT FUNCTION IM STRUGGLING WITH				
    mclListener.onLoadInit = function(target){
    	var widthHolder = target._width;
     	target._x = position;
        	position += target._width + 1;
    	
           imgWidth.unshift(widthHolder);				
      };

    As far as I understand, I can't use my 'for' loop to store the image widths because they don't have width until they are initialized. As you can see, I'm not using push, but unshift, because for some reason the onLoadInit fires in reverse order of the load, but I think that is an unrelated factor. I would love any help or suggestions for alternate methods. Thanks.

  2. #2
    AS3 Mod
    Join Date
    Sep 2007
    Location
    O-H-I-O
    Posts
    2,385
    This should work.
    PHP Code:
    // A FEW OF THE VARS, AND THE MCL WITH LISTENER
    var position:Number 0;
    var 
    loader:MovieClipLoader = new MovieClipLoader();
    var 
    i:Number 0;
    var 
    mclListener:Object = new Object();
    var 
    widthHolder:Array = new Array(); // YOU DID NOT DECLARE THE ARRAY?!?!?

    //THE LOOP THAT PULLS THE IMAGES AND PLACES THEM INTO COVER_MC
    for(i=0i<imageArray.lengthi++){
        var empty = 
    _mp3Player.cover_mc.createEmptyMovieClip("image"+i,i);
        
    loader.loadClip(imageArray[i],empty);
    };

    //THE ONLOADINIT FUNCTION IM STRUGGLING WITH
    mclListener.onLoadInit = function(target){
        var 
    widthHolder target._width;
        
    target._x position;
        
    position += target._width 1;
        
        
    widthHolder.unshift(imgWidth);// YOU HAD THIS BACKWARDS??!?!?
    };

    loader.addListener(mclListener); 

  3. #3
    Junior Member
    Join Date
    Jan 2008
    Posts
    24
    Ah, no, I should have been more clear. I was a little sloppy with my copy and pasting. I have most of my variables declared at the top of my document so I didn't copy all of my definitions when I posted. I've added the variables I left out. sstalder, I can see what you're saying, but I don't think my 'unshift' is backwards. imgWidth is the Array, widthHolder is a temporary var that I was using to trace the image widths at various points. I still can't figure this one out, any more suggestions?

    PHP Code:
    // A FEW OF THE VARS, AND THE MCL WITH LISTENER
    var position:Number 0;
    var 
    loader:MovieClipLoader = new MovieClipLoader();
    var 
    i:Number 0;
    var 
    mclListener:Object = new Object();
    var 
    imgWidth:Array = new Array();


    //THE LOOP THAT PULLS THE IMAGES AND PLACES THEM INTO COVER_MC
    for(i=0i<imageArray.lengthi++){
        var empty = 
    _mp3Player.cover_mc.createEmptyMovieClip("image"+i,i);
        
    loader.loadClip(imageArray[i],empty);
    };

    //THE ONLOADINIT FUNCTION IM STRUGGLING WITH
    mclListener.onLoadInit = function(target){
        
        
    //DECLARE TEMP VAR TO STORE THE WIDTH
        
    var widthHolder target._width;
        
    target._x position;
        
    position += target._width 1;
        
        
    //WHEN I TRACE WIDTHHOLDER RIGHT HERE, I GET THE APPROPRIATE    WIDTHS
        //BUT WHEN I TRACE THE IMGWIDTH ARRAY, I GET UNDEFINED
        
    imgWidth.unshift(widthHolder);
    };

    loader.addListener(mclListener); 

  4. #4
    Junior Member
    Join Date
    Jan 2008
    Posts
    24
    Ah, very strange. I got it to work below. Maybe someone who knows more about how flash stores arrays and initializes movie clip loader can explain this to me. Even though I had declared all my arrays, flash didn't recognize them by the time onLoadInit fired. I got around it by creating the array at the same time the images finished loading.

    PHP Code:
    var position 0;
    var 
    loader = new MovieClipLoader();
    var 
    0;
    var 
    mclListener:Object = new Object();
    var 
    0;
                    
    for(
    i=0i<imageArray.lengthi++){
        var empty = 
    _mp3Player.cover_mc.createEmptyMovieClip("image"+i,i);
        
    imgInstance.push(empty);
            
    loader.loadClip(imageArray[i],empty);
        };
                    
        
    mclListener.onLoadInit = function(target){
            if(
    c==0){
                
    imgWidth = new Array;
                
    c++;
                }
                    
    target._x position;
                    
    position += target._width 1;
                
    imgWidth.unshift(target);
            };
        
    loader.addListener(mclListener); 

  5. #5
    AS3 Mod
    Join Date
    Sep 2007
    Location
    O-H-I-O
    Posts
    2,385
    Ah I missed the most important thing, this should work properly:

    PHP Code:
    // A FEW OF THE VARS, AND THE MCL WITH LISTENER 
    var position:Number 0
    var 
    loader:MovieClipLoader = new MovieClipLoader(); 
    var 
    i:Number 0
    var 
    mclListener:Object = new Object(); 
    var 
    imgWidth:Array = new Array(); 


    //THE LOOP THAT PULLS THE IMAGES AND PLACES THEM INTO COVER_MC 
    for(i=0i<imageArray.lengthi++){ 
        var empty = 
    _mp3Player.cover_mc.createEmptyMovieClip("image"+i,i); 
        
    loader.loadClip(imageArray[i],empty); 
    }; 

    //THE ONLOADINIT FUNCTION IM STRUGGLING WITH 
    mclListener.onLoadInit = function(target,imgWidth){ // Need to pass array variable through function
         
        //DECLARE TEMP VAR TO STORE THE WIDTH 
        
    var widthHolder target._width
        
    target._x position
        
    position += target._width 1
         
        
    //WHEN I TRACE WIDTHHOLDER RIGHT HERE, I GET THE APPROPRIATE    WIDTHS 
        //BUT WHEN I TRACE THE IMGWIDTH ARRAY, I GET UNDEFINED 
        
    imgWidth.unshift(widthHolder); 
    }; 

    loader.addListener(mclListener); 

  6. #6
    Junior Member
    Join Date
    Jan 2008
    Posts
    24
    Thanks for sticking with me on this sstalder. I thought passing the array into that function should have worked, but I still get an undefined array. I think the onLoadInit function treats everything as a local variable, so using arrays that are declared outside of it doesn't work.

    The only other idea I had was to get the onLoadInit function to return the width of the image, and then I could push it into my array outside the onLoadInit, but I can't figure out if that function can return anything.

    I think I'm going to need to rework the structure of my project a little. I was thinking that I could just build the whole thing inside the onLoadInit function, or does that seem like a bad idea?

  7. #7
    AS3 Mod
    Join Date
    Sep 2007
    Location
    O-H-I-O
    Posts
    2,385
    When I get back from lunch I will do some testing, I know it's possible but we are missing something else here.

  8. #8
    Senior Member
    Join Date
    Aug 2002
    Location
    Dublin, Ireland
    Posts
    1,749
    Had a quick glance through and you have a fairly serious code error in your function:
    Code:
    mclListener.onLoadInit = function(target){
        var widthHolder = target._width;
        target._x = position;
        position += target._width + 1;
        
        widthHolder.unshift(imgWidth);// YOU HAD THIS BACKWARDS??!?!?
    };
    You have declared widthHolder as an array outside of this function, and then redeclare widthHolder inside the function as a local variable that you set to the width. Then you try to unshift the local var number into itself which won't work for any number of reasons including that Number() doesn't have an unshift method...

    I think you probably mean...
    Code:
    mclListener.onLoadInit = function(target){
        var imgWidth = target._width;
        target._x = position;
        position += target._width + 1;
        
        widthHolder.unshift(imgWidth);// YOU HAD THIS BACKWARDS??!?!?
    };

  9. #9
    Senior Member
    Join Date
    Aug 2002
    Location
    Dublin, Ireland
    Posts
    1,749
    Sorry, I think you confused me... I'm right about the problem, but is it imgWidth or widthHolder that you want as the array?

  10. #10
    AS3 Mod
    Join Date
    Sep 2007
    Location
    O-H-I-O
    Posts
    2,385
    Ok, this works.
    PHP Code:
    // A FEW OF THE VARS, AND THE MCL WITH LISTENER
    var position:Number 0;
    var 
    loader:MovieClipLoader = new MovieClipLoader();
    var 
    i:Number 0;
    var 
    mclListener:Object = new Object();
    var 
    imgWidth:Array = new Array();


    //THE LOOP THAT PULLS THE IMAGES AND PLACES THEM INTO COVER_MC
    for(i=0i<imageArray.lengthi++){
        var empty = 
    _mp3Player.cover_mc.createEmptyMovieClip("image"+i,i);
        
    loader.loadClip(imageArray[i],empty);
    };

    mclListener.onLoadInit = function(target) {
        
    target._x position;
        
    position += target._width 1;
        
        
    addWidth(target._width);
    };

    function 
    addWidth(w) {
        
    imgWidth.unshift(w);
    }

    loader.addListener(mclListener); 

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center