user780756
user780756

Reputation: 1484

AS3 Preloader doesn't work locally, loaderInfo ProgressEvent.PROGRESS event does not fire

Making a custom AS3 preloader, I noticed that when my SWF is executed locally (file:///) the preloader gets stuck in the loading screen when previewed in a web browser like Chrome.

When executed from a remote server or through the standalone Flash Player, then it works. I noticed other SWF that have preloaders do not have this issue. What I need to change?

this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, preloaderProgress);

function preloaderProgress(event:ProgressEvent):void {

    var loadedPercent:Number = event.bytesLoaded/event.bytesTotal*100;

    if (loadedPercent == 100){
        this.gotoAndStop(2);
    }
}

Upvotes: 0

Views: 803

Answers (2)

BotMaster
BotMaster

Reputation: 2223

Loading locally goes very fast. Not only you should check if the file is completely loaded using the Event.COMPLETE and not the ProgessEvent but you should also make sure to register your listeners before actually calling load or the file might end up completely loaded before you register your listeners.

Upvotes: 1

user780756
user780756

Reputation: 1484

Following the example at http://stephenscholtz.com/201110/single-movie-flash-preloading-as3

It seems that there was no ProgressEvent.COMPLETE, but there is a Event.COMPLETE instead, a bit confusing.

I changed my code to the following, (also including some tweaks for right click menu to prohibit the user from right clicking and pressing Play before movie has loaded and such)

//Initialize any variables
var loadedPercent:Number = 0;

//Remove all items from right click Flash Player menu (except Quality, and the mandatory About... & Settings...)
var cxMenu:ContextMenu = new ContextMenu();
cxMenu.hideBuiltInItems();
cxMenu.builtInItems.quality = true;
this.contextMenu = cxMenu;

/* or disable right click menu completely (Flash Player 11.2.202.228 and over) */
/* this.addEventListener(MouseEvent.RIGHT_CLICK, onMouseRightClick);
function onMouseRightClick(event:MouseEvent) 
{
    return false;
} */

//Disable shortcut keys and window menubar when played from desktop Standalone Flash Player app
if (Capabilities.playerType == "StandAlone" || Capabilities.playerType == "External") {
    fscommand("showmenu", "false");
    fscommand("trapallkeys", "true");
}

/* Preloader: begin */

//Update loading bar and percent text as SWF loads
function onProgress(e:Event) {

    //Get the amount of bytes that have been loaded / bytes total to load
    loadedPercent = this.loaderInfo.bytesLoaded/this.loaderInfo.bytesTotal*100;

    //Update the text of _preloaderProgress movieclip
    _preloaderProgress.text = int(loadedPercent)+"%";

    //Update the _preloaderBar amount by scaling horizontally as it loads
    _preloaderBar.scaleX = loadedPercent/100;

}

//Go to next frame after everything loads
function init(e:Event) {
    gotoAndStop(2);
}

//Attach the events to the stage
this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
this.loaderInfo.addEventListener(Event.COMPLETE, init);

/* Preloader: end */

//Stop to 1st frame and wait until it is loaded
stop();

This will allow the movie to play both remotely and locally with no problems.

Upvotes: 0

Related Questions