views:

354

answers:

2

Hey guys, I have a bit of code here that pulls, at random, 50 images from a file and pushes them into an array. I now need to pull, at random, 350 times from the array. I'm using a timer in place of the for loop to grab the images from the array. My code works fine as long as I do not use an array and I loop the images straight from the file. But that's very slow and bad form. I think I can do everything except the .load of the array. I can't seem to make it work. I'm getting errors that say "#1009: Cannot access a property or method of a null object reference" or "1067: Implicit coercion of a value of type Array to an unrelated type flash.net:URLRequest". I think I get the point of these messages, but I cannot seem to figure out how to pull the URLRequests back out of the array. Please, any help is much appreciated.

var imgLoader:Loader; 
var imgSource:URLRequest; 
var imgArrayer:Array = new Array(); 
var imgNum:uint; 
var timer1Count:uint; 
var thumbFade:Tween; 
var layerCount:uint = 0;  



for(var i:uint = 0; i < 50; i++) {
    imgNum = Math.random() * _imgCount;
    imgSource = new URLRequest("thumbsFinal/img"+imgNum+".jpg"); 
    imgArrayer.push(imgSource);
}


var myTimer:Timer = new Timer(_imgTrTime, _imgTrInt); 
    myTimer.addEventListener(TimerEvent.TIMER, timedFunction);
    myTimer.start();


function timedFunction(e:TimerEvent):void{
    imgLoader.load(imgArrayer);
    thumbFade = new Tween(imgLoader, "alpha", Regular.easeIn, _thumbFaderB, _thumbFaderF, _thumbFaderSpd, true);
    addChildAt(imgLoader, layerCount);
    imageAdjust();
    timer1Count++;
    layerCount++;


        if(timer1Count == _scrnFadeTimer) {
            screenFade();
        }
        else if(timer1Count == _txtDeploy){
            textTween();
        } 
}
A: 

You need to loop back through you imgArrayer array.

for (var i:uint = 0; i < imgArrayer.length; i++) {
    imgLoader.load(imgArrayer[i]);
    ...
}

Also, you need to either cast as int, or use a Math function to handle

imgNum = Math.random() * _imgCount;

which will assign imgNum as a float, instead of an int.

Math.floor|Math.round|Math.ceil|int(Math.random() * _imgCount);

sberry2A
I think I see what you guys are saying but I can't seem to get that piece to work. I just edited my post to include all of this section of, I should have done this to begin with though, sorry.
acslater
Do this, `imgLoader.load(imgArrayer[myTimer.currentCount-1]);` just to see it working. This will cause errors eventually as your timer count goes over the length of the array, but there are lots of ways to handle that.
sberry2A
From the start I get "Error #1009: Cannot access a property or method of a null object reference." errors. I've tried a bunch of different things now to simply pass the URL to the .load and the string makes it there but as soon as it goes to the .load it gives me that error. I've even copied and pasted other code from tutorials in to my code and I get the same error from my loader. Do you think my built in classes are messed up or something?
acslater
A: 

this bit might be:

for(var i:uint = 0; i < 50; i++) {
    imgNum = Math.floor(Math.random() * _imgCount); 
    imgSource = new URLRequest("thumbsFinal/img"+imgNum+".jpg"); 
    imgArrayer.push(imgSource);
}

and the bit that pulls 350 times would be:

imgLoader.load(imgArrayer[Math.floor(Math.random() * (imgArrayer.length))]);

called in that timer 350 times

George Profenza
oh...same thing sberry2A :) u get the picture :)
George Profenza