So I'm trying to make a preloader. Problem is, I don't know exactly what I'm doing. I adapted some code from a tutorial, but it currently isn't working.
Any help would be greatly appreciated!
stop(); preloadBtn.enabled=false; preloadBtn.visible=false; preloader.addEventListener(Event.COMPLETE, onPreloaderComplete); preloader.setLoaderInfo(loaderInfo); function onPreloaderComplete(e:Event):void { preloadBtn.enabled=true; preloadBtn.visible=true; preloadBtn.addEventListener(MouseEvent.CLICK, preloadToMenu); } function preloadToMenu(event:MouseEvent):void { preloadBtn.removeEventListener(MouseEvent.CLICK, preloadToMenu); gotoAndStop(âmenuâ); } function setLoaderInfo(ldrInf:LoaderInfo):void { ldrInf.addEventListener(ProgressEvent.PROGRESS, onProgress); ldrInf.addEventListener(Event.COMPLETE, onComplete); } function onProgress(e:ProgressEvent):void { var percent:int=Math.round(e.bytesLoaded/e.bytesTotal*100); preloader.preloadermask.width=5.02*percent; percentText.text=percent+â%â; trace(percent); } function onComplete(e:Event):void { dispatchEvent(e); }
This is a rip from the default preloader FlashDevelop creates if you create a new AS3 project with preloader, maybe it helps
package { import flash.display.DisplayObject; import flash.display.MovieClip; import flash.events.Event; import flash.events.ProgressEvent; import flash.utils.getDefinitionByName; /** * ... * @author */ public class Preloader extends MovieClip { public function Preloader() { addEventListener(Event.ENTER_FRAME, checkFrame); loaderInfo.addEventListener(ProgressEvent.PROGRESS, progress); // show loader } private function progress(e:ProgressEvent):void { // update loader } private function checkFrame(e:Event):void { if (currentFrame == totalFrames) { removeEventListener(Event.ENTER_FRAME, checkFrame); startup(); } } private function startup():void { // hide loader stop(); loaderInfo.removeEventListener(ProgressEvent.PROGRESS, progress); var mainClass:Class = getDefinitionByName("Main" as Class; addChild(new mainClass() as DisplayObject); } } }
What this actually does is preload your movie, and once it's done, it starts your main class. Perhaps it's not completely what you're looking for, but it might give you some ideas Also, though I'm not completely sure, you could also add an event listener at loaderInfo for the Event.DONE event - from the top of my head, it should dispatch it once it's done loading... might be a bit more accurate than checking for the amount of loaded frames, dunno. I've heard of issues where all the bytes were loaded, but not everything was initialized - leading to some pretty nasty errors!
I managed to get it done dropping the event.Complete and just going with an ENTER_FRAME listener as Gluesy recommended. Took a bit of wrangling but I got it to work.
Thanks Pixel; that's more class-based. The game I'm currently on is not class-based, so it was done on the main timeline. I have learned a lot since I started, and so I've kind of figured out that it's not the best idea to program on the main timeline. Next time will be different.