Preloader not working !!!
Copy link to clipboard
Copied
Hi All,
I used 2 preloaders code, but both are not working !!!, what problem with this code ?
script 1
//-------------------------------------------------------------------------------------------------------------------------------
stop();
this.addEventListener(Event.ENTER_FRAME, loading);
function loading(e:Event):void
{
var total:Number = this.stage.loaderInfo.bytesTotal;
var loaded:Number = this.stage.loaderInfo.bytesLoaded;
loader.kolam.loadTxt.text = Math.floor((loaded/total)*100);
if (total == loaded)
{
this.removeEventListener(Event.ENTER_FRAME, loading);
loader.nextFrame()
}
}
//-------------------------------------------------------------------------------------------------------------------------------
script 2
var preLoaderPercent:Number;
this.loaderInfo.addEventListener(Event.COMPLETE, loadComplete);
this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress);
function loadComplete(e:Event)
{
this.loaderInfo.removeEventListener(Event.COMPLETE, loadComplete);
this.loaderInfo.removeEventListener(ProgressEvent.PROGRESS, loadProgress);
loader.nextFrame();
}
function loadProgress(e:ProgressEvent):void
{
preLoaderPercent = e.bytesLoaded / e.bytesTotal;
loader.kolam.loadTxt.text = preLoaderPercent;
}
????????
Copy link to clipboard
Copied
You need what is typically called a "stub". Until your document is loaded there is no way for that code to execute. The ENTER_FRAME event won't be firing until the document is done loading. It's different than AS2, in other words.
What you can do is create a separate SWF that acts as a preloader. They're typically tiny in size (~10k). If you use this approach you'll have access to downloading the other SWF and the progress while doing so because the ~10kb tiny preloader will have fully loaded and will fire off Event.ENTER_FRAME (as well as progress events, etc).
Copy link to clipboard
Copied
I am wondering, in as3 Its not possible to have preloader on same flash ?
Copy link to clipboard
Copied
You could say as3 is fixed for expected behavior. Why should an ENTER_FRAME event fire off before the document is even loaded?
A preloader is always a good idea. Give it a try. Being there's no more as2 nonsense about using _root to gain access to movieclips and code, a preloader won't screw up your code anymore by being what we used to think of as "_root".
Try building the stub as I mentioned and use the Loader class to load that SWF. Here's a tutorial.
e.g.
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
// init a new loader
var preload:Loader = new Loader();
// listen for loading complete
preload.contentLoaderInfo.addEventListener(Event.COMPLETE, preloadCompleteHandler);
// listen for errors
preload.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, preloadErrorHandler);
// listen for progress!
preload.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, preloadProgressHandler);
// load my content
preload.load(new URLRequest("myContent.swf"));
// handlers
// handle complete
function preloadCompleteHandler(e:Event):void
{
// load complete! add to display list, remove preloader visuals, etc
addChild(preload); // a loader is a display object, you can add it directly
}
// handle error
function preloadErrorHandler(e:IOErrorEvent):void
{
// do what you wish if there is an error loading the SWF
trace(e);
}
// progress handler
function preloadProgressHandler(e:ProgressEvent):void
{
// display the progress in some way
var bytesLoaded:int = e.bytesLoaded; // current bytes loaded
var bytesTotal:int = e.bytesTotal; // total bytes to load
// now do what you want, like ver a percent loaded
var percent:Number = bytesLoaded / bytesTotal * 100;
trace(percent + " loaded");
}
Copy link to clipboard
Copied
Can we stop the loaded swf animation ? Since i am not loading it right away, preloaded has 10sec animation !!!
FYI: Loaded swf has 2 frames, animations on the second frame only !
Copy link to clipboard
Copied
Yes you just need access to the MovieClip contained in the loader. Anything publicly exposed (like any frame scripts, etc) are accessible.
In the example above you don't need to addChild(preload) once it's complete. You could just set an interval to wait after the preload is complete and change it to frame 2, e.g.:
import flash.utils.setTimeout; // need to import it
function preloadCompleteHandler(e:Event):void
{
// preloading is complete, wait 10 seconds then run a function
setTimeout(loadAnimation, 10000); // this is in milliseconds, 10,000 is 10 seconds
}
function loadAnimation():void
{
addChild(preload);
// the .content property of a loader is the MovieClip
MovieClip(preload.content).gotoAndStop(2); // go to frame 2 and stop
}
Copy link to clipboard
Copied
hello so i used the script in the first example for a preloader. i keep getting the error message "
Scene=Scene 1, layer=Actions, frame=1, Line 5 | The class or interface 'Event' could not be loaded. |
" I have no idea what this means or how to fix it. can you help me? any advice would be greatly appreciated.
Copy link to clipboard
Copied
The Event class can be imported (at the top of your code) via:
import flash.events.Event;
Copy link to clipboard
Copied
oh ok thank you so much
Copy link to clipboard
Copied
You're welcome and good luck!
Copy link to clipboard
Copied
ok so i'm still getting that error message plus this one:
Scene=Scene 1, layer=Actions, frame=1, Line 1 | The class or interface 'flash.events.Event' could not be loaded. |
this is what my code looks like:
import flash.events.Event;
stop();
this.addEventListener(Event.ENTER_FRAME, loading);
function loading(e:Event) : void{
var total:Number = loaderInfo.bytesTotal;
var loaded:Number = loaderInfo.bytesLoaded;
if (loaded==total){
this.removeEventListener(Event.ENTER_FRAME, loading);
gotoAndStop (2);
}else{
Loading_Bar.scaleX = loaded/total;
Loading_Txt.text = Math.floor((loaded/total)*100) + "%";
loader.nextFrame()
}
}
Copy link to clipboard
Copied
If you're doing this in the IDE, technically you don't even need this line because Flash is smart enough to know you're going to need it and will self-include it.
What version of Flash are you using? What version of Flash Player are you targeting? Being in the AS3 forums I hope I can assume you're targeting AS3.0.
Try opening a brand new AS3.0 document in Flash. Just add the ENTER_FRAME loop and have it do something basic like trace("hello");. Run it and see if you see the trace. If so there's something fundamentally wrong with your other project.

Copy link to clipboard
Copied
Hey- if you still have issues on flash runtime ,would you pls log a bug to
https://bugbase.adobe.com/, thanks.
When adding the bug, please include sample code and the exactly browser(version) you used so we can quickly test this out internally. If you would like to keep this private, feel free to e-mail the attachment to me (jiyuan@adobe.com) directly and I will attach it to the bug for internal use only.

