Game Development Reference
In-Depth Information
The actual preloading is handled in a very simple, straightforward manner. We add a BitmapData
background ( appBackBD inside appBackBitmap ) and a TextField ( textfield ) to display a status to
the user. We also create two event listener functions: checkFrame listens for the Event.EnterFrame
event and progress listens for the ProgressEvent.PROGRESS of the LoaderInfo class.
The progress function simply updates the textField with the current percentage loaded for the
Main class, and the checkFrame function waits until the first frame has been loaded and the play
head has moved to the fake frame 2 created with the compiler directive described earlier. When
checkFrame detects that the movie is on frame 2 ( currentFrame == totalFrames ), the startup
function is called.
The startup function removes the appBackBitmap and textField from the display list and adds an
instance of the Main class to the display list. We must make some changes to the constructor and
the init functions in Main for this to work properly.
Finally, we glue this to our old code by creating an instance of Main and add it to the display list of
the stage. Here is the code that does that magic:
var mainClass:Class = getDefinitionByName("com.efg.games.tunnelpanic.Main") as Class;
addChild(new mainClass() as DisplayObject);
So now, you see that preloading with the Flex SDK actually requires a second class (Preloader)
and a special compiler directive “-frame start com.efg.games.tunnlepanic.Main ) to effectively
preload the Main and all the embedded assets.
Preloading in the Flash IDE
Preloading our game in the Flash IDE is different than doing it with the Flex SDK. The primary
differences are in how the timeline and library assets interact with the preload process. For this
example, we are going to use the assets needed for this chapter's game, Tunnel Panic. We need
to import three sounds into our library for the game. We have created our own sounds and music
specifically for this game. If you have the game assets from this topic's web site, you can use
those. If not, you will have to supply three sounds: a title screen music clip, a music clip to play
during game play, and a sound for when the player ship is destroyed.
Adding files to the library
You will want to add your three sounds to the library, as shown in Figure 12-2. You will see that
there is also a MovieClip called SoundHolder in the library; we'll discuss that one in a bit.
Search WWH ::




Custom Search