the Loader Works with the timelineAfter the game is preloaded, the GameLoop document class will play the movie, forcing it to pass over frame 2, and sit on frame 3. Flash has now initialized all of the objects in frame 2, and even though they are not physically on frame 3: (notice below that only frame 2 of the assets layer needs to have the assets on the stage).We sit the play head on frame 3 with no assets off stage to interfere with our game play, speed, etc.The codeWe need 2 classes to make this work. They are pretty simple. One is the GameLoop.as and the other is the LoadingBox.as. The loading box is a simple encapsulation of the functionality needed to display the 100% loaded on the screen. GameLoop is a very basic implementation of a State Driven loop (not an OO state machine, but a simple state pattern). I will explain the code in detail next:Here is all of the GameLoop.as code.[cc lang="javascript" width="550"]/*** ...* @author Jeff Fulton* @version 0.1*/package {import flash.display.MovieClip;import LoadingBox;import flash.events.*;import flash.media.Sound;import flash.media.SoundChannel;public class GameLoop extends MovieClip {public static const STATE_SYSTEM_LOADER:int=0;public static const STATE_SYSTEM_TITLE:int=1;public var gameState:int=STATE_SYSTEM_LOADER;public var loadingBox:LoadingBox;public var titleScreen:TitleScreen;public var loaderStarted:Boolean=false;public var titleStarted:Boolean=false;public var percentLoaded:int;public var titleMusic:SoundMusic1;public var titleMusicChannel:SoundChannel;public function GameLoop():void {addEventListener(Event.ENTER_FRAME, runGame);}public function runGame(e:Event):void {switch (gameState) {case STATE_SYSTEM_LOADER:doLoadScreen();break;case STATE_SYSTEM_TITLE:doTitleScreen();break;}}public function doLoadScreen():void {trace((this.loaderInfo.bytesLoaded/this.loaderInfo.bytesTotal)*100);if (!loaderStarted) {if (this.loaderInfo.bytesLoaded == this.loaderInfo.bytesTotal) {finishPreload();}else{loadingBox=new LoadingBox();this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, loadingProgress);this.loaderInfo.addEventListener(Event.COMPLETE, loadingComplete);loaderStarted=true;loadingBox.x=200;loadingBox.y=200;addChild(loadingBox);}}}public function loadingProgress(e:Event):void {percentLoaded=(this.loaderInfo.bytesLoaded/this.loaderInfo.bytesTotal)*100;trace(percentLoaded);loadingBox.update(percentLoaded);}public function loadingComplete(e:Event) {trace("loadingComplete");removeChild(loadingBox);loadingBox=null;this.loaderInfo.removeEventListener(Event.COMPLETE loadingComplete);this.loaderInfo.removeEventListener(ProgressEvent.PROGRESS, loadingProgress);finishPreload();}public function finishPreload():void {this.play();gameState=STATE_SYSTEM_TITLE;}public function doTitleScreen():void {if (!titleStarted && currentFrame==3) {titleMusic= new SoundMusic1();titleScreen=new TitleScreen();titleScreen.x=50;addChild(titleScreen);titleMusicChannel=titleMusic.play();titleStarted=true;}}} // end class} // end package[/cc]I'm not going to go into detail on how to play sounds, instantiate library objects or any of the other standard Flash things I need to so to make this simple loader have something to load, but I will go into detail on how the gameLoop state machine interacts with the loader and allows the game to proceed through the two included states : STATE_SYSTEM_LOADER and STATE_SYSTEM_TITLE;The state pattern:Our GameLoop state is controlled by two things. The first is a int variable called gameState. The second is therunGame() method. We define two states as Static Const class variables for our two states. We define them as Static for future use. If we have other classes and objects that need access to these states (and in my gameLoop design I always do), then we can reference them without needing to create another instance of the GameLoop class. In this instance, the GameLoop would be considered part of the Singleton design pattern, but we are not going to go out of our way to follow all of the rules for using the Singleton pattern in this simple example. The two values that the gameState variable can contain are STATE_SYSTEM_LOADER and STATE_SYSTEM_TITLE.The ConstructorIn this simple gameLoop example, the constructor does little more than create an event to run our runGame method constantly. There certainly are better ways to create an actual game loop, and one can be found in my last tutorial Creating an Optimized AS3 Game Timer Loop. The event.ENTER_FRAME event is just the simplest method to get a