Flash Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsWeb DesignFlash Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 19th, 2008, 08:28 PM
Frank Grimes's Avatar
Frank Grimes Frank Grimes is offline
Plays with fire
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: Outside looking in
Posts: 494 Frank Grimes User rank is Sergeant (500 - 2000 Reputation Level)Frank Grimes User rank is Sergeant (500 - 2000 Reputation Level)Frank Grimes User rank is Sergeant (500 - 2000 Reputation Level)Frank Grimes User rank is Sergeant (500 - 2000 Reputation Level)Frank Grimes User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 4 Days 2 h 7 m 5 sec
Reputation Power: 24
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:
1086: Syntax error: expecting semicolon before leftbracket.


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

Reply With Quote
  #2  
Old July 19th, 2008, 11:22 PM
benno32's Avatar
benno32 benno32 is offline
/*
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 498 benno32 User rank is Major (30000 - 40000 Reputation Level)benno32 User rank is Major (30000 - 40000 Reputation Level)benno32 User rank is Major (30000 - 40000 Reputation Level)benno32 User rank is Major (30000 - 40000 Reputation Level)benno32 User rank is Major (30000 - 40000 Reputation Level)benno32 User rank is Major (30000 - 40000 Reputation Level)benno32 User rank is Major (30000 - 40000 Reputation Level)benno32 User rank is Major (30000 - 40000 Reputation Level)benno32 User rank is Major (30000 - 40000 Reputation Level)benno32 User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 33 m 39 sec
Reputation Power: 391
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|

Reply With Quote
  #3  
Old July 20th, 2008, 12:55 AM
benno32's Avatar
benno32 benno32 is offline
/*
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 498 benno32 User rank is Major (30000 - 40000 Reputation Level)benno32 User rank is Major (30000 - 40000 Reputation Level)benno32 User rank is Major (30000 - 40000 Reputation Level)benno32 User rank is Major (30000 - 40000 Reputation Level)benno32 User rank is Major (30000 - 40000 Reputation Level)benno32 User rank is Major (30000 - 40000 Reputation Level)benno32 User rank is Major (30000 - 40000 Reputation Level)benno32 User rank is Major (30000 - 40000 Reputation Level)benno32 User rank is Major (30000 - 40000 Reputation Level)benno32 User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 33 m 39 sec
Reputation Power: 391
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:
Original - ActionScript Code
  1. //An Array with 3 elements in it
  2. var myArray:Array = new Array(3);
  3. //Where to place our next triangle
  4. var triangleOffset:Number = 0;
  5.  
  6. //Loop through the array and create/add to the stage a new triangle
  7. //offset 100px to the right of the last triangle we created.
  8. for(var i:Number = 0; i < myArray.length; i++)
  9. {
  10.     //Call our propSprite function
  11.     propSprite();
  12.     //Adjust the offset of the next triangle
  13.     triangleOffset += 100;
  14. }
  15.  
  16. function propSprite()
  17. {
  18.     var triangle:Sprite = new Sprite();//New Sprite object
  19.    
  20.     with (triangle.graphics)//call multiple graphic methods on our 'triangle' sprite
  21.     {
  22.         lineStyle(0);//Will create a hairline black line as our stroke
  23.         beginFill(0xFF9900,0.5);//sets an orange fill with a 50% opacity
  24.         moveTo(50, 0);//start our line at these co-ords
  25.         lineTo(100, 100);//start drawing the triangle on its own canvas
  26.         lineTo(0, 100);//add the second side
  27.         lineTo(50, 0);//add the third side
  28.         endFill();//close up the fill
  29.     }
  30.     //Position our triangle
  31.     triangle.x = triangleOffset;
  32.     //Add the triangle to the stage
  33.     this.addChild(triangle);
  34. }

Reply With Quote
  #4  
Old July 20th, 2008, 04:54 AM
Tann San Tann San is offline
Gotta get to the next screen..
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Nov 2003
Location: Legion of Dynamic Discord
Posts: 4,713 Tann San User rank is Colonel (50000 - 60000 Reputation Level)Tann San User rank is Colonel (50000 - 60000 Reputation Level)Tann San User rank is Colonel (50000 - 60000 Reputation Level)Tann San User rank is Colonel (50000 - 60000 Reputation Level)Tann San User rank is Colonel (50000 - 60000 Reputation Level)Tann San User rank is Colonel (50000 - 60000 Reputation Level)Tann San User rank is Colonel (50000 - 60000 Reputation Level)Tann San User rank is Colonel (50000 - 60000 Reputation Level)Tann San User rank is Colonel (50000 - 60000 Reputation Level)Tann San User rank is Colonel (50000 - 60000 Reputation Level)Tann San User rank is Colonel (50000 - 60000 Reputation Level)Tann San User rank is Colonel (50000 - 60000 Reputation Level)  Folding Points: 9974 Folding Title: Novice Folder
Time spent in forums: 3 Weeks 1 Day 10 h 25 m 58 sec
Reputation Power: 580
Facebook MySpace
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"));
__________________
-Tann

-Vote for your favorite ActionScript editor here.

Reply With Quote
  #5  
Old July 20th, 2008, 09:05 AM
Frank Grimes's Avatar
Frank Grimes Frank Grimes is offline
Plays with fire
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: Outside looking in
Posts: 494 Frank Grimes User rank is Sergeant (500 - 2000 Reputation Level)Frank Grimes User rank is Sergeant (500 - 2000 Reputation Level)Frank Grimes User rank is Sergeant (500 - 2000 Reputation Level)Frank Grimes User rank is Sergeant (500 - 2000 Reputation Level)Frank Grimes User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 4 Days 2 h 7 m 5 sec
Reputation Power: 24
Thanks, everyone. This works great.

Quote:
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


Thanks, Benno. I'm still amazed at how my mind works very late in the day.

And Tann:

Quote:
// Might as well add it as a child now and do away with the array from before
this.addChild(loader);


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.

Reply With Quote
  #6  
Old July 20th, 2008, 12:06 PM
Frank Grimes's Avatar
Frank Grimes Frank Grimes is offline
Plays with fire
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: Outside looking in
Posts: 494 Frank Grimes User rank is Sergeant (500 - 2000 Reputation Level)Frank Grimes User rank is Sergeant (500 - 2000 Reputation Level)Frank Grimes User rank is Sergeant (500 - 2000 Reputation Level)Frank Grimes User rank is Sergeant (500 - 2000 Reputation Level)Frank Grimes User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 4 Days 2 h 7 m 5 sec
Reputation Power: 24
Hm...I'm noticing an error when I run your example, Tann:

Quote:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at sortis_fla::MainTimeline/onComplete()


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

Reply With Quote
  #7  
Old July 20th, 2008, 12:26 PM
Tann San Tann San is offline
Gotta get to the next screen..
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Nov 2003
Location: Legion of Dynamic Discord
Posts: 4,713 Tann San User rank is Colonel (50000 - 60000 Reputation Level)Tann San User rank is Colonel (50000 - 60000 Reputation Level)Tann San User rank is Colonel (50000 - 60000 Reputation Level)Tann San User rank is Colonel (50000 - 60000 Reputation Level)Tann San User rank is Colonel (50000 - 60000 Reputation Level)Tann San User rank is Colonel (50000 - 60000 Reputation Level)Tann San User rank is Colonel (50000 - 60000 Reputation Level)Tann San User rank is Colonel (50000 - 60000 Reputation Level)Tann San User rank is Colonel (50000 - 60000 Reputation Level)Tann San User rank is Colonel (50000 - 60000 Reputation Level)Tann San User rank is Colonel (50000 - 60000 Reputation Level)Tann San User rank is Colonel (50000 - 60000 Reputation Level)  Folding Points: 9974 Folding Title: Novice Folder
Time spent in forums: 3 Weeks 1 Day 10 h 25 m 58 sec
Reputation Power: 580
Facebook MySpace
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 :¬)

Reply With Quote
  #8  
Old July 21st, 2008, 08:24 AM
Frank Grimes's Avatar
Frank Grimes Frank Grimes is offline
Plays with fire
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: Outside looking in
Posts: 494 Frank Grimes User rank is Sergeant (500 - 2000 Reputation Level)Frank Grimes User rank is Sergeant (500 - 2000 Reputation Level)Frank Grimes User rank is Sergeant (500 - 2000 Reputation Level)Frank Grimes User rank is Sergeant (500 - 2000 Reputation Level)Frank Grimes User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 4 Days 2 h 7 m 5 sec
Reputation Power: 24
No problem. You've earned a *little* forgiveness.

I do appreciate the help!

:grimey

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignFlash Help > ActionScript 3 - Load multiple images


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |