ForumsProgramming ForumPreloader Help

6 3223
rjbman
offline
rjbman
215 posts
Nomad

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);
}
  • 6 Replies
gluesy
offline
gluesy
65 posts
Nomad

addEventListener(Event.ENTER_FRAME, loading);
function loading(e:Event):void {

var total:Number=this.stage.loaderInfo.bytesTotal;
var loaded:Number=this.stage.loaderInfo.bytesLoaded;

bar_mc.scaleX = (loaded/total)*30;
loader_txt.text = String("Loaded "+Math.floor((loaded/total)*100)+ "%"

if (total==loaded) {
removeEventListener(Event.ENTER_FRAME, loading);
play()
}
}


This code should work.
bar_mc is your loading bar movieclip and loader_text is a text box for which the percentage done is down.

Like kurt said remember to click simulate download and set your download settings according to your swf size.
rjbman
offline
rjbman
215 posts
Nomad

No, it currently shows 0% but the mask is full. So I'm thinking its something with the syntax. I'll try gluesy's recommendation tomorrow.

Then we have spring break so guess who has two thumbs and gets a week without flash? This guy!

PixelSmash
offline
PixelSmash
566 posts
Nomad

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&quot 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!

Anyway, good luck!

rjbman
offline
rjbman
215 posts
Nomad

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.

PixelSmash
offline
PixelSmash
566 posts
Nomad

Ahh, no worries You don't want to see the first code I've written lol... probably a stop(); on the main timeline!

Showing 1-5 of 6