Saturday, September 08, 2007

Very Important Event in AS3: Event.ADDED_TO_STAGE

Event.ADDED_TO_STAGE is SO important it's ridiculous I took me so long to find it.
If you're creating a swf that will be loaded by an external loader, you need this event to be trigger before even thinking about accessing any of the stage properties.
With this event you can access the stage from the loaded movieclip.

If you don't check this event, stage will only return null.


Loader:

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

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

function loadProgress(event:ProgressEvent):void {
var percentLoaded:Number = event.bytesLoaded/event.bytesTotal;
percentLoaded = Math.round(percentLoaded * 100);
trace("Loading: "+percentLoaded+"%");
}
function loadComplete(event:Event):void {
trace("Complete");
addChild(loader); // when this is done this will fire the Event.ADDED_TO_STAGE event
}


Loaded content class:


public function yourConstructor(){
this.addEventListener(Event.ADDED_TO_STAGE, init);
}

public function init(event:Event):void{
trace(stage.quality)
}

Labels: , ,


This page is powered by Blogger. Isn't yours?