|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
ActionScript 3 - Load multiple images
Hi--
I'm trying load multiple images, but am having a spot of trouble. If I understand the theory correctly, each image should have its own loader() object, but I'm getting an error on line 14: Quote:
Line 14 is: Code:
var loaders[i]:Loader = new Loader(); All the code: Code:
var thumbs:Array=["1.jpg","2.jpg","3.jpg","4.jpg","5.jpg","6.jpg","7.jpg","8.jpg"];
var array:Array = new Array();
var sprites:Array = new Array();
var loaders:Array = new Array();
var thumbsContainer:Sprite = new Sprite();
var cowY:Number=0;
for (var i=0;i<thumbs.length;i++) {
var loaders[i]:Loader = new Loader();
loaders[i].contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loaders[i].load(new URLRequest(thumbs[i]));
}
function onComplete(e:Event):void {
trace(i);
sprites[i]=new Sprite();
sprites[i].addChild(imgLoader);
sprites[i].width=50;
sprites[i].height=50;
sprites[i].y=cowY;
addChild(sprites[i]);
cowY+=15;
}
__________________
“Be ashamed to die until you have won some victory for humanity.” -- Horace Mann "...all men are created equal." -- US Declaration of Indepenence |
|
#2
|
||||
|
||||
|
Wouldn't it be more efficient for you to use functions to create only one new instance of a loader and sprite object--then loop your array of images through those functions? Perhaps this would fix your problem and make things more smoother.
Also in your onComplete function it looks like you are trying to propagate multiple sprites; however your sprite[i] variables aren't within a loop ![]()
__________________
---| The Universe| |
|
#3
|
|||||
|
|||||
|
In case what I said didn't make sense. Heres some basic code to help illustrate what I meant about using functions to create only one new instance of an object rather than using multiple arrays.
Here I have just one array with three elements in it and a function that creates one triangle. i use one loop to go through my array and produce three triangles. ActionScript Code:
|
|
#4
|
|||
|
|||
|
Hi, you need to change the line to this:
loaders[i] = new Loader(); hmm I think you can change it all to: Code:
import flash.display.Sprite;
var thumbs:Array = ["1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg", "6.jpg", "7.jpg", "8.jpg"];
var cowY:int = 0;
var loader:Loader;
for(var i:int = 0; i < thumbs.length; i++)
{
loader = new Loader();
loader.y = cowY;
cowY += 15;
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.load(new URLRequest(thumbs[i]));
// Might as well add it as a child now and do away with the array from before
this.addChild(loader);
}
// The only thing we do in here is set the dimensions to squash the image down
function onComplete(e:Event):void
{
(e.target as Sprite).width = 50;
(e.target as Sprite).height = 50;
}
If you are using sequentially numbered thumbnails and they all have the same extension then you could do away with the array of names and just use an integer of their count. Here the bits you could swap out: Code:
// var thumbs:Array = ["1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg", "6.jpg", "7.jpg", "8.jpg"]; var thumb_count:int = 8; // for(var i:int = 0; i < thumbs.length; i++) for(var i:int = 1; i <= thumb_count; i++) // loader.load(new URLRequest(thumbs[i])); loader.load(new URLRequest(i + ".jpg")); |
|
#5
|
||||
|
||||
|
Thanks, everyone. This works great.
Quote:
Thanks, Benno. I'm still amazed at how my mind works very late in the day. ![]() And Tann: Quote:
That's interesting, too. Adding the child before it's loaded. I'm assuming it's adding an empty child container if it's not loaded, then just fills it when the image is loaded. Correct? Thanks again! This really opens my eyes! :grimey PS-- I'm not sure what format the images will be in. I'm just using sequential files for testing. The customer hasn't decided about this; they may come in from XML or a PHP script or even a pre-determined set. |
|
#6
|
||||
|
||||
|
Hm...I'm noticing an error when I run your example, Tann:
Quote:
I see 8 of these. My fix was changing the code in the onComplete function to this: Code:
e.target.content.width = 50; e.target.content.height = 50; since e.target was only referencing the loader object. Thanks! :grimey |
|
#7
|
|||
|
|||
|
ah, sorry about that, I tend to make spactastic mistakes when I try and write more then 10 lines of code in the quick reply box :¬)
|
|
#8
|
||||
|
||||
|
No problem. You've earned a *little* forgiveness.
![]() I do appreciate the help! :grimey |
![]() |
| Viewing: Dev Shed Forums > Web Design > Flash Help > ActionScript 3 - Load multiple images |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|