PDA

View Full Version : Load Image - for loop issue



tjsl2
March 26th, 2009, 02:11 PM
I Have to say, I'm totally bamboozled by this one. I've only just jumped from AS2 to AS3 so I'm sure it must be me doing something really stupid.

I'm loading in a bunch of images with a for loop. The images do appear on stage so that part works, however, spacing them is the problem.

this is what the following code traces out:

"undefined [object MovieClip] 1
undefined [object MovieClip],[object MovieClip] 2
undefined
undefined"

Why is _newImgArray[loadCount] undefined when _newImgArray exists and loadCount has a value.




public function loadProjects():void{

_newImgArray = new Array();

for(var i:Number = 1; i< _projects.length; i++){

var imageHolder:MovieClip = new MovieClip();
addChild(imageHolder);
var _imageString:String = _projects[i].thumbnail;
imageLoader = new Loader();
imageLoader.load(new URLRequest(_imageString));
imageHolder.addChild(imageLoader);
_newImgArray.push(imageHolder);
loadCount = i;
trace(_newImgArray[loadCount] + " " + _newImgArray + " " + loadCount);
imageLoader.contentLoaderInfo.addEventListener(Eve nt.COMPLETE, newImageLoaded);
}
}

private function newImageLoaded($e:Event):void{

trace(_newImgArray[loadCount]);
//_newImgArray[loadCount].y = 100 * loadCount;
//_newImgArray[loadCount].x = 15;
imageLoader.contentLoaderInfo.removeEventListener( Event.COMPLETE, newImageLoaded);
}
Please could someone tell me where I'm going wrong before my computer ends up going through the window next to me.

irrationalistic
March 26th, 2009, 02:17 PM
The problem is that arrays are 0 based. This means that if you have one item in your array, it is at index 0, not index 1. Your loadCount variable is equal to "i" which starts at 1! I'd say, make the following change:


loadCount = i - 1;

Hope that fixes it!

tjsl2
March 26th, 2009, 02:56 PM
Ahh, of course, silly mistake. I was trying to access the 2nd item in my XML and totally forgot I purposely had not added the 1st xml item to the array. Anyway, all working now, thanks so much for your help. My computer lives another day.

tjsl2
March 26th, 2009, 03:31 PM
... although the code I've written for spacing my images vertically doesn't work. I'll carry on playing around with it, but if anyone can see the reason why the x and y values are only changing for the last item in the array, I'd love to know.

snickelfritz
March 26th, 2009, 04:18 PM
I think I would set the xy position of imageLoader within the for..loop before adding it to the imageHolder.

tjsl2
March 27th, 2009, 11:12 AM
Hi,

thanks that did work, however...

I failed to explain properly that as the image sizes vary, I need the height of the images to space them evenly, hens attempting to space them when the image existed in the COMPLETE event. I've re written it slightly and it now works. I realized I didn't need the for loop at all, as loading the images sequentially could be triggered with the Event.COMPLETE. It kind of feels like I'm making this up as I go along so if anyone knows a better way to do this I'd be interested to see.




public function loadProjects():void{

var imageHolder:MovieClip = new MovieClip();
addChild(imageHolder);
var _imageString:String = _projects[loadCount].thumbnail;
imageLoader = new Loader();
imageLoader.load(new URLRequest(_imageString));
imageHolder.addChild(imageLoader);
_newImgArray.push(imageHolder);
imageLoader.contentLoaderInfo.addEventListener(Eve nt.COMPLETE, newImageLoaded);
}

private function newImageLoaded($e:Event):void{

imageLoader.contentLoaderInfo.removeEventListener( Event.COMPLETE, newImageLoaded);
loadCount++;
_projHeight = $e.target.content.height;

trace(_projHeight);
_heightArray.push(_projHeight);

if(loadCount < _projects.length){
loadProjects();
}else if(loadCount == _projects.length){
spacing();
}
}

private function spacing():void{

for(var i:Number = 0; i< _newImgArray.length; i++){
//space images here
}
}