We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Flash CS4/AS 3.0 Array question

Medium Priority
709 Views
Last Modified: 2013-11-12
Hello,

I am hoping someone can shed some light...I would like to preload 15 gifs and put them in an Array and then display them on the stage later.  I have the code below.  However, it only loads the last gif and puts it in the array, so there is only 1 gif in the array instead of 2.  I am not sure what to do...???

Thanks a lot!

Cheers!
var gifArray = new Array();
for(var i:Number=1; i<3; i++){
var l:Loader = new Loader();
l.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loop);
l.contentLoaderInfo.addEventListener(Event.COMPLETE, done);
l.load(new URLRequest("OC"+i+".gif"));
}
									
 
 
function done(e:Event):void
{
	gifArray.push(l);
	trace("Complete!! - " + i);
	gifArray[0].x = 100;
	addChild(gifArray[0]);
	addChild(gifArray[1]);
 
}

Open in new window

Comment
Watch Question

It's cos you're trying to add "l" o the array - add (e.target as LoaderInfo).loader instead, ie:

gifArray.push((e.target as LoaderInfo).loader);

Author

Commented:
When I tried to substitute your code in my code below (that I finally got to work...), it throws an error:

ReferenceError: Error #1056: Cannot create property x on flash.display.LoaderInfo.
      at content_fla::MainTimeline/done()
Complete!! - 3
ReferenceError: Error #1056: Cannot create property x on flash.display.LoaderInfo.
      at content_fla::MainTimeline/done()

Your way seems much more elegant, but don't know what I did wrong....my code (that is working) is below.  Any ideas?

Thanks for your comments.
var loadArray = new Array();
var gifArray = new Array();
for(var i:Number=1; i<3; i++){
loadArray[i] = new Loader();
loadArray[i].contentLoaderInfo.addEventListener(Event.COMPLETE, done);
loadArray[i].load(new URLRequest("OC"+i+".gif"));
}
									
 
function done(e:Event):void
{
	for(var j:Number=1; j<3; j++){
	gifArray.push(loadArray[j]);
	}
	trace("Complete!! - " + i);
	gifArray[0].x = 100;
	gifArray[1].y = 100;
	addChild(gifArray[0]);
	addChild(gifArray[1]);
	//removeChild(gifArray[0]);
}

Open in new window

You need to tell it what the LoaderInfo is, eg:

MovieClip((e.target as LoaderInfo).loader)

Author

Commented:
I'm sorry--you've lost me.  I don't know where to insert "MovieClip".  All I know is once my code has : gifArray.push (e.target as LoaderInfo, loadArray); it doesn't bomb, but I am unable to access the individual gifs from the array.  I cannot assign x values etc.  Then it throws the error above.  If I "trace(gifarray) with your line of code I get:

Complete!! - 3
[object LoaderInfo],,[object Loader],[object Loader]
Complete!! - 3
[object LoaderInfo],,[object Loader],[object Loader],[object LoaderInfo],,[object Loader],[object Loader]

I understand you told me to tell it it is a MovieClip, but I don't know where I have to do that.  

Thanks~
Sorry - I wasn't very specific...

gifArray.push (MovieClip(e.target as LoaderInfo, loadArray));

Author

Commented:
Just to add, I have tried:

gifArray.push(Bitmap(e.target as LoaderInfo), loadArray);

and

gifArray.push(MovieClip(e.target as LoaderInfo), loadArray);

And, I get the error :

TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::LoaderInfo@413ef941 to flash.display.MovieClip.
      at content_fla::MainTimeline/done()
TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::LoaderInfo@413ef821 to flash.display.Bitmap.
      at content_fla::MainTimeline/done()

I am loading gifs, so they are not MovieClips.  I would like to understand how to do it your way, but it seems I may just have to settle for doing it the way I got it to work--

If you can see what I am doing wrong, it would be much appreciated....

Author

Commented:
Hi again,

Tried to copy and paste your code and I get an error:

1137: Incorrect number of arguments.  Expected no more than 1.
Sorry - last thing I wrote was rubbish! I wrote it in a rush.

Try:

gifArray.push(MovieClip((e.target as LoaderInfo).loader))

Author

Commented:
Hi,

I get this error:

TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::Loader@55d090e1 to flash.display.MovieClip.
      at content_fla::MainTimeline/done()
TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::Loader@55d09191 to flash.display.MovieClip.
      at content_fla::MainTimeline/done()

Author

Commented:
Hi,

Changed the code to :

gifArray.push(MovieClip((e.target as LoaderInfo).loadArray)), since my loader is name loadArray and I get the compile error:

1119: Access of possibly undefined property loadArray through a reference with static type flash.display:LoaderInfo.
Sorry for wasting your time - I'm annoyed with myself now!

The following is actually tested!

gifArray.push(Bitmap((e.target as LoaderInfo).content));

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
yes, that did the trick!  Thanks so much for your patience and help!

Cheers!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.