• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 467
  • Last Modified:

AS3 variable:URLRequest for loop

how do you loop through some code creating a new URLrequest eg:

for(var i:uint = 0; i < 5; i++)
	{
var req+[i]:URLRequest = new URLRequest("images/" +[i] + ".jpg");
  var loader+[i]:Loader = new Loader();
      loader+[i]:.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, preLoad+[i]:);
		
	}

Open in new window

I hope this is clear

Thanks
0
nth1971
Asked:
nth1971
  • 8
  • 7
1 Solution
 
blue-genieCommented:
is there a reason why you want to create a new URLRequest with each loop?

i assume you're trying to load a bunch of external images in a loop?
have a look here, it's quite a tidy way of handling it.

http://bkwld.com/blog/2011/03/load-multiple-images-in-as3-by-storing-loader-information-in-an-object/
0
 
nth1971Author Commented:
Thanks bg, but i want to do my own as im learning, please advise if im a million miles away (i've reworked it a little from the link you provided)

i need each image that has loaded to remain on the stage...this is what i have so far, it seems that the last image loaded is the only one that displays... ? i appreciate any input as i am new to as3 and would like to learn ...

	 //create empty movie clip to load images into
		var displayContainer_mc:MovieClip = new MovieClip();
		displayContainer_mc.x = 50;
		displayContainer_mc.y = 135;
		displayContainer_mc.name = "displayContainer_mc"; 
		addChildAt(displayContainer_mc,0);




	 
	//create array and store images in
	var _imageArray:Array = new Array();	
	for(var i:uint = 0; i < 5; i++)
{
		//load images
		 	_imageArray[i] = "images/"+[i]+".jpg"; 
		 
		//create a URLRequest 
			var req1:URLRequest = new URLRequest(_imageArray[i]);		
			 
		//create a new loader
			var loader1:Loader = new Loader();
			
		// add progress listener run function
			loader1.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, preLoad);	
		
    		loader1.load(req1)
	
	 
	 
	 

	function preLoad(event:ProgressEvent):void
	{	
		//trace(counter++);
			var percent:Number = Math.round(event.bytesLoaded / event.bytesTotal * 100);
		// preLoad_txt1.text = String(percent) + "%";
			if (percent == 100)
			trace(loader1.name)
		{
		
		
		//i'm getting lost here i think, in adding the loader1 to the stage it replaces itself each time and i need
		//to keep all images on the stage
		displayContainer_mc.addChildAt(loader1,0);		
		loader1.x = 50;		
		loader1.name = "img"+[i]
		}	
	} 				 
}

Open in new window

0
 
blue-genieCommented:
no you're not a million miles away and you're right about what is happening.
remember in a loop, you're always going to end up at the last item of the loop - hence in your case the last image.

i understand you say you don't want to follow a tutorial so some questions and things to think about.
1. when using the ProgressEvent, that's fine if you want to display a preloader data, however when you want to add the child to the stage (or in your case the movieclip) - rather use the Event.INIT event.

2. there's something odd in your syntax if(percent ==100) part with the }{ but you're not using else etc? it's not throwing any errors but it's odd :-)

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
nth1971Author Commented:
i have removed the odd syntax and added a Event.INIT event... but to no avail,

i still overwrite the img loaded with the next img that loads in the loop

I'm sure to be missing something fundamental and i went through the code again in the link you provided but im stuck! anymore help would be great thanks

0
 
blue-genieCommented:
oh, sorry i thought you didn't want the answer was just looking for pointers after your comment about the tutorial.
what is the ultimate effect you want to achieve, stick x number of images onto the stage?
is there a specific reason why you're adding it to another movieclip?

can you upload your fla - leave the images.
upload as CS4.
tx.
0
 
nth1971Author Commented:
I'm trying to load a first image to the stage, whilst that image has loaded another loads in the background, when the second image has loaded a third loads in the background and so on....

After a specific time (approx 5 secs) a simple transition from the first image to the second

wait (approx 5 secs) then transition to third... wait... 4th image and so on until we reach 5 or so images and we go back to first

might perhaps use the tweening to give each image a little scroll make it look nice...

I'm still learning and would like to take the time needed but when your stuck... your stuck!

Thanks
homeScreen-.fla
0
 
blue-genieCommented:
i need to finish a project for a client for Monday, will have a look after that if no one gets back to you before then.
0
 
nth1971Author Commented:
Appreciate it very much... thanks, I've got the scroll done and pretty much everything else... I'm going to hammer away at it, I'm finding as3 quite a challenge but rewarding all at the same time!
0
 
nth1971Author Commented:
i hope you can help still? I've been working on it, I'm almost there (I think) I get the images to load in but a weird timing intervals I'd appreciate if you could look at the code and advise

      
 //create empty movie clip to load images into - I thought this might 
	 //assist when i try to implemet a transition effect later
		var displayContainer_mc:MovieClip = new MovieClip();
		displayContainer_mc.x = 0;
		displayContainer_mc.y = 135;
		displayContainer_mc.name = "displayContainer_mc"; 
		addChildAt(displayContainer_mc, 0);
		

	 //pre loader graphic, basic at the mo but will jazz up later
		 var _loader:preLoader = new preLoader();
		_loader.x = 500;
		_loader.y = 135;
		

		 var loader:Loader = new Loader();
		 
	 	 var counter:Number = 0	 
	 //create array and store images in ################
		 var _imageArray:Array = new Array();	
			for(var i:uint = 0; i < 5; i++)
		{
	 //load images
		_imageArray[i] = "images/"+[i]+".jpg"; 
		trace(_imageArray[i])
	 //#####
	 
		var req:URLRequest = new URLRequest(_imageArray[counter]);
		}
	 //################################################
		
		
		function preLoad(event:ProgressEvent):void
		{			
			addChild(_loader);	
			trace("here");		
		}
		
		loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, preLoad);
			loader.contentLoaderInfo.addEventListener(Event.INIT, imgShow);
			loader.load(req)
			//imgTimer1.stop();
	//##################################################
		/*function loadEmUp(event:Event):void
		{
			
		}*/
		//##################################################

		function imgShow(event:Event):void
		{ 
			removeChild(_loader);		
			displayContainer_mc.addChild(loader);		
			var imgScrollIn:Tween = new Tween(loader,"x",Strong.easeOut,0,-200,4,true);
			//imgScrollIn.stop();
			var imgAlphaIn:Tween = new Tween(loader,"alpha",Strong.easeOut,0,1,4,true);			
			imgScrollIn.addEventListener(TweenEvent.MOTION_FINISH, startTimer);			
		}
		
		function startTimer(event:Event):void
		{
		// We need to import the utils package
		// Create a new Timer
		var imgTimer:Timer = new Timer(5000);
		imgTimer.addEventListener("timer", imgTimerFunction);
		
		// Start the timer
		imgTimer.start();
		}	
		
		// Function will be called every 5000 milliseconds
		function imgTimerFunction(eventArgs:TimerEvent)
		{
			counter++
			trace(counter);
			trace("load next image");			
			var req:URLRequest = new URLRequest(_imageArray[counter]);			
			loader.load(req)
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, preLoad);			
		}

Open in new window

0
 
blue-genieCommented:
i haven't forgotten about you, just over a week behind schedule at the office. will check tonight when i get home.
0
 
nth1971Author Commented:
I've tried using the link you provided

http://bkwld.com/blog/2011/03/load-multiple-images-in-as3-by-storing-loader-information-in-an-object/ 

and i cant get it to work... I really need to get this done so if anyone can help? Thanks
0
 
blue-genieCommented:
hey sorry it took so long but have a look at the zip file i uploaded.

this will show you how to load multiple images. this uses tween lite so make sure you've setup your class paths accordingly.

example.zip
0
 
nth1971Author Commented:
wow, thanks. Still going through the code and absorbing it and trying to understand exactly what is happening, its very clear, thank you so much!

The tweenLite,  its so much smoother that timeline animation and just looks and feels more professional. Its exactly what I've been trying to achieve using flash transitions but with more scope... I can see I have a lot more to do!

0
 
nth1971Author Commented:
very very helpful
0
 
blue-genieCommented:
you're very welcome!
the guys at greensock are amazing. keep an eye on their site for their wonderful work they do, and they share it for free :-)

there's really on point doing timeline animations these days. (IMO)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now