Skip to main content
Known Participant
March 27, 2014
Answered

Preloading assets

  • March 27, 2014
  • 1 reply
  • 335 views

Hi,

I'm making a flash app for the web and have a lot of images and sound in it. I would like to preload these assets before the program loads so that the program runs smoothly without having to load assets on the spot

Here's my code:

function preLoader():void

{

          for(var i:int = 1; i <= 6; i++)

          {

                    for(var j:int = 1; j <= (this["array"+i][0].length)-1; j++)

                    {

                              snd1 = new Sound(new URLRequest(this["array"+i][0]));

  snd1.addEventListener(Event.COMPLETE, preLoadComplete);

  image1.url = this["array" + i][1];

                              imageLoader1.load(image1);

  imageLoader1.contentLoaderInfo.addEventListener(Event.COMPLETE, preLoadComplete);

                    }

 

          }

}

function preLoadComplete(e:Event):void

{

          count++;

  if(count == totalAssetNo)

   {

     init();

   }

}

All the paths for the images and sounds are stored in arrays.

The sounds preload fine but the images don't. The images only call the preLoadComplete function once in total not once each time something has loaded

Is this the correct way of preloading?

How can I correctly preload the images?

Cheers

Chris

This topic has been closed for replies.
Correct answer kglad

you need to wait until loading is complete for each image before starting to load the next image because a loader can only load one item at any one time.  ie, remove those for-loops and manually increment i and j in preloadComplete.

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
March 27, 2014

you need to wait until loading is complete for each image before starting to load the next image because a loader can only load one item at any one time.  ie, remove those for-loops and manually increment i and j in preloadComplete.