Flash CS5 / AS3, after preloading main class constructor can't reference movieclips
I modified an Adobe Flash CS5 sample to create a swf with a preloader.
In my FLA I've two stopped frames:
In the first frame I only put this code (and a textfield showing percentage):
stop();
this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, onLoading);
this.loaderInfo.addEventListener(Event.COMPLETE, onComplete);
function onLoading(evt:ProgressEvent):void {
var loaded:Number = evt.bytesLoaded / evt.bytesTotal;
percent_txt.text = (loaded*100).toFixed(0) + "%";
};
function o开发者_开发技巧nComplete(event:Event):void {
this.loaderInfo.removeEventListener(ProgressEvent.PROGRESS, onLoading);
this.loaderInfo.removeEventListener(Event.COMPLETE, onComplete);
gotoAndStop(2);
};
In the second frame I:
- exported the Main class;
- I have all the needed graphics assets on stage;
When I test the Adobe sample (it has no main class on frame 2, only a large image) anything works fine, but when I compile the modified version I get strange errors.
In the Main class constructor I reference three movieclips, eg. this way: myClip.alpha=0
, but it seems now Flash can't see them anymore (they are null). Why?
How can I make this preloader work?
Thanks in advance.
When you say Main class, are you referring to a class you have set as the Document Class? If I'm following you correctly, the problem is likely that the Document Class is always instantiated on the first frame, so your instances on stage wouldn't yet exist.
It's not possible to have the Document Class wait to instantiate until later frames. You'll probably have to remove your class from the Document Class assignment in order to create your instance on frame 2. At that point, you can pass a reference to your movieclips or stage through to the constructor of your class.
精彩评论