Posts Tagged ‘actionscript 3’

Actionscript 3 External Preloader

Wednesday, December 19th, 2007

A preloader is an important part of any sizeable Flash app as you probably don’t want your users to see nothing while the app’s loading. There are two types of preloaders that you can employ: external and internal. In this tutorial we’ll see how to implement External Preloaders, which load the contents from a separate file. This method is much cleaner and easier than the internal way, so I recommend you use it whenever you can. It also allows you to keep your preloader and main content independent of each other.

The general idea is to set up a Loader object in your preloader SWF, and make it execute 2 functions: an update function, and a finish function. The update function is called every time load progress is made, and the finish function is called when loading is done. You would accomplish this through Event Listeners as follows:

var request:URLRequest = new URLRequest("Content.swf");
var loader:Loader = new Loader();

loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);

What you do inside those functions is totally up to you. If you have a loading animation that animates relatively to the progress (like a cup filling up), then you would update that animation inside the loadProgress function. But as an example, we’ll be updating a percent counter as the load progresses.

function loadProgress(event:ProgressEvent):void 
    var percentLoaded:Number = event.bytesLoaded / event.bytesTotal;
    percentLoaded = Math.round(percentLoaded * 100);
    this.percentLoaded.text = String(uint(percentLoaded)) + "%";

function loadComplete(event:Event):void 
    trace("Load Complete");

To wrap it all up, we call load on the URL request, and add the Loader object to the preloader’s Display List, so that the content shows up when it’s done loading.


Also, make sure that the preloader’s dimension and frame rate settings are the same as the main content’s.

Here are the source files with the external preloader, and a test content SWF.

External Preloader

Test content SWF

Music Looping in AS3

Monday, December 10th, 2007

According to the AS3 documentation you can loop a sound by passing in a loop number argument when you call play on a sound. Apparently, that never seems to work consistently for me. Sometimes it would not loop after a number of times, or it doesn’t loop immediately. So I usually just manually control the looping of music with Event Listeners.

To do this, I add an Event Listener to the music channel that will call a function to replay the sound whenever it’s done playing once.

public function playMusic():void
    musicChannel =; 
    musicChannel.addEventListener(Event.SOUND_COMPLETE, loopMusic);

public function loopMusic(e:Event):void
    if (musicChannel != null)
        musicChannel.removeEventListener(Event.SOUND_COMPLETE, loopBackgroundMusic);

So the sound will continue to loop forever until I stop it. Whenever I do, I have to remove the Event Listener from the music channel or else it won’t be garbage collected.

public static function stopMusic():void
    if (musicChannel != null)
	musicChannel.removeEventListener(Event.SOUND_COMPLETE, loopMusic);

That’s about it. A pretty simple solution to an annoying problem sometimes.