I’m writing a preloader:
package {
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.text.TextField;
import flash.display.LoaderInfo;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.system.System;
public class Preloader extends Sprite {
private var t:TextField = new TextField();
private var ver:String = "1000";
public function Preloader() {
t.y = 570;
addChild( t );
t.text = ver;
addEventListener( Event.ADDED_TO_STAGE, init );
}
private function init( e:Event ):void {
this.root.loaderInfo.addEventListener( ProgressEvent.PROGRESS, onLoadingProgress );
this.root.loaderInfo.addEventListener( Event.COMPLETE, onLoadingCompleted );
// See if it's already completed
var percent:int = int( root.loaderInfo.bytesLoaded / root.loaderInfo.bytesTotal );
if ( percent == 1 )
onLoadingCompleted();
}
private function onLoadingProgress( event:Event ):void {
var percent:int = int(root.loaderInfo.bytesLoaded / root.loaderInfo.bytesTotal * 100);
t.text = "Loading.. " + percent + "%";
}
private function onLoadingCompleted( event:Event = null ):void {
root.loaderInfo.removeEventListener( ProgressEvent.PROGRESS, onLoadingProgress );
root.loaderInfo.removeEventListener( Event.COMPLETE, onLoadingCompleted );
var mainClass:Class = loaderInfo.applicationDomain.getDefinition("Main") as Class;
var main:DisplayObject = new mainClass() as DisplayObject;
parent.addChild( main );
parent.removeChild( this );
}
}
}
with this as the Main class:
package {
import flash.display.Sprite;
public class Main extends Sprite {
public function Main() {
}
}
}
so this is close to as barebones as I could make it.
However, it greets me with a:
ReferenceError: Error #1065: Variable Main is not defined.
at flash.system::ApplicationDomain/getDefinition()
at ...
I use frames.frame to insert Main already. I am compiling using ant and the linux SDK directly (mxmlc). Am I missing anything obvious?
When you’re making a preloader in this “style” what really happens is that the preloader is put in the first frame of the application, and the rest in a second frame. What you’re missing here is to tell the compiler that you want your Main class compiled in, so right now it doesn’t even exist in the swf. That’s why getDefinition won’t work.
Nor can you simply refer to it in the preloader since that would make it load in the first frame before the preloader can be shown. So, you need a little custom argument magic.
Add this line to your compiler options and you should be good to go:
Remember that if your Main class is in a package you need to get a full reference in there:
Same goes for the getDefinition call.
EDIT:
When looking over my code that does this I see that I use a different approach from what you did, maybe this works better:
EDIT AGAIN:
If it works using a button I’d guess the complete event is fired too early for some reason. It may be so that everything is not inited properly event though all the bytes are loaderd. Try using this code to check for completion instead:
It might also be a good idea to add a stop() command in your onLoadingCompleted() method just to make the playhead won’t be screwing things up, but that’s a later issue really.