Preloading in Actionscript 3 with 2 swf’s

Here’s an example document class that belongs to a preloading swf that loads in my main application swf.

package com.deceptiveresolution.demo.swf.preloading
{
import flash.text.TextField;
import flash.events.ProgressEvent;
import flash.events.Event;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.display.MovieClip;
import flash.display.Sprite;

public class PreloadingContainer extends MovieClip
{

private var __container:Sprite;
private var __preloader:Loader;
public var percent_txt:TextField;
private static const __APP_URL:String = “TextChat.swf”;

public function PreloadingContainer()
{
trace(”\nPreloadingContainer()”);
__preloader = new Loader();
__preloader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, _onPreloadProgress, false, 0, true);
__preloader.contentLoaderInfo.addEventListener(Event.INIT, _onPreloadInit, false, 0, true);
__preloader.contentLoaderInfo.addEventListener(Event.COMPLETE, _onPreloadComplete, false, 0, true);
__preloader.load(new URLRequest(__APP_URL));
}

private function _onPreloadProgress(e:ProgressEvent):void
{
trace(”\nPreloadingContainer._onPreloadProgress()”);
var percentage:Number = e.bytesLoaded / e.bytesTotal;
percent_txt.text = String(Math.ceil(percentage * 100));
}

private function _onPreloadInit(e:Event):void
{
trace(”\nPreloadingContainer._onPreloadInit()”);
percent_txt.text = “100″;
}

private function _onPreloadComplete(e:Event):void
{
trace(”\nPreloadingContainer._onPreloadComplete()”);
removeChild(percent_txt);
percent_txt = null;
__container = Sprite(__preloader.content);
addChild(__container);
__preloader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, _onPreloadProgress);
__preloader.contentLoaderInfo.removeEventListener(Event.INIT, _onPreloadInit);
__preloader.contentLoaderInfo.removeEventListener(Event.COMPLETE, _onPreloadComplete);
__preloader = null;
}

Advertisements

2 responses to “Preloading in Actionscript 3 with 2 swf’s

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: