Game Development Reference
In-Depth Information
Adding the new systemPreload and addSounds functions
The systemPreload function makes sure the entire SWF file has been loaded before the play
head moves to frame 3 of the timeline. By moving to frame 3, the play head passes over frame 2
and allows code-level access to all of the assets contain inside the asset holder located on that
frame.
private function systemPreload():void {
if (!preloaderStarted) {
trace("preload started");
preloadScreen.setDisplayText("Loading: 0%"); //Changed chapter 12
addChild(preloadScreen)
preloaderStarted=true;
}else{
var percentLoaded:int=(this.loaderInfo.bytesLoaded/
this.loaderInfo.bytesTotal)*100;
trace(percentLoaded);
preloadString="Loading: "+percentLoaded+"%";
preloadScreen.setDisplayText(preloadString);
if (percentLoaded >99) {
trace(">99");
this.play();
}
if (currentFrame==3) {
trace("frame == 3")
addSounds();
removeChild(preloadScreen);
nextSystemState = FrameWorkStates.STATE_SYSTEM_TITLE;
switchSystemState(nextSystemState);
}
}
}
This function begins by checking the preloaderStarted Boolean variable. If that's false, it sets up
the preloadScreen instance and places it on the screen. If the Boolean is true, it jumps down and
monitors the preload of the SWF file.
The monitoring is a very simple process. The percentLoaded local variable is calculated and used
to display text back to the user on the preloadScreen. The percentLoaded variable is also used to
determine how much of the SWF file has been loaded. If more than 99 percent of the file has
been loaded, the state is changed to the FrameWorkStates.STATE_SYSTEM_TITLE state. The play
function is called to move the play head from frame 1 to frame 2 (and beyond).
Why 99 percent? In some rare occasions, Flash does not register that a file has loaded exactly
100 percent. In our experience, testing to make sure that more than 99 percent is loaded often
helps resolve this issue. If you are uncomfortable with this, change the code to if
(percentLoaded >=100) .
Once the play function has been called, we wait for the currentFrame property of the SWF to be
equal to 3 before we can move forward with our game. The call to the SoundManager.addSound
that instantiates the sounds in the library can all be triggered now. This will be part of the
addSounds function. Also, we can switch the state machine to the next state.
Search WWH ::




Custom Search