Created
April 2, 2010 21:59
-
-
Save troygilbert/353779 to your computer and use it in GitHub Desktop.
Preloader Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package | |
{ | |
import flash.display.Sprite; | |
import flash.events.Event; | |
public class Application extends Sprite | |
{ | |
/** Constructor. **/ | |
public function Application() | |
{ | |
// this doesn't have to be called from the constructor, | |
// it can be called whenever the app is ready for the | |
// preloader to be removed; don't forget to bubble it! | |
dispatchEvent(new Event(PreloaderDemo.REMOVE_PRELOADER, true)); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package | |
{ | |
import flash.display.DisplayObject; | |
import flash.display.Graphics; | |
import flash.display.MovieClip; | |
import flash.display.Sprite; | |
import flash.display.StageAlign; | |
import flash.display.StageScaleMode; | |
import flash.events.Event; | |
import flash.utils.getDefinitionByName; | |
[SWF(width="800", height="600", frameRate="60", backgroundColor="0x000000")] | |
public class PreloaderDemo extends MovieClip | |
{ | |
/** Bubble this event up to the stage to remove the preloader. **/ | |
public static const REMOVE_PRELOADER:String = "removePreloader"; | |
/** Add this class to frame 2 with the compiler argument: -frame 2 Application **/ | |
protected static const APP_CLASS_NAME:String = "Application"; | |
/** Container for our preloader graphics/animation. **/ | |
protected var preloader:Sprite; | |
/** Constructor. **/ | |
public function PreloaderDemo() | |
{ | |
stop(); | |
addEventListener(Event.ENTER_FRAME, onEnterFrame); | |
// pixel-perfect, no scaling, aligned top-left | |
stage.align = StageAlign.TOP_LEFT; | |
stage.scaleMode = StageScaleMode.NO_SCALE; | |
// preloader animation | |
preloader = new Sprite(); | |
addChild(preloader); | |
} | |
/** Check loading status each frame. **/ | |
protected function onEnterFrame(event:Event):void | |
{ | |
// update the preloader with the loading progress | |
var g:Graphics = preloader.graphics; | |
g.clear(); | |
// draw a solid background so we can't see the app as it loads in the background | |
g.beginFill(0x000000); | |
g.drawRect(0, 0, stage.stageWidth, stage.stageHeight); | |
g.endFill(); | |
// draw the outline of a progress bar | |
g.lineStyle(3, 0xffffff, 1, true); | |
g.drawRoundRect(100, 290, 600, 20, 10, 10); | |
// fill the progress bar based on how many of our bytes have been loaded | |
var progress:Number = loaderInfo.bytesloaded / loaderInfo.bytesTotal; | |
g.beginFill(0xffffff); | |
g.drawRoundRect(100, 290, 600 * progress, 20, 10, 10); | |
g.endFill(); | |
// check if all of the frames of the SWF have loaded | |
if (framesLoaded == totalFrames) | |
{ | |
// if you want the preloader to show additional progress information, | |
// make changes here to listen for progress events from the application you create | |
removeEventListener(Event.ENTER_FRAME, onEnterFrame); | |
nextFrame(); | |
createApplication(); | |
} | |
} | |
/** Create the application. **/ | |
protected function createApplication():void | |
{ | |
// we have to start listening for this *before* we create our application | |
// because it's constructor may dispatch the "remove preloader" event | |
addEventListener(REMOVE_PRELOADER, onRemovePreloader); | |
// must create the application by name so we don't have any static linkage to this class | |
var appClass:Class = Class(getDefinitionByName(APP_CLASS_NAME)); | |
var app:DisplayObject = new appClass(); | |
// we add the application underneath our preloader | |
// this allows us to keep the preloader displayed until the app is ready to go | |
addChildAt(app, 0); // add app underneath the preloader | |
} | |
/** Remove preloader. **/ | |
protected function onRemovePreloader(event:Event):void | |
{ | |
removeEventListener(REMOVE_PRELOADER, onRemovePreloader); | |
removeChild(preloader); | |
preloader = null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment