It can be very easy to get carried away with the fun part of the game and forget all the usability stuff like the menu and game shell (the chrome on the outside of the play area). I want to very quickly discuss a bit of structure for our display list and create a simple intro animation followed by a menu (just a play button for now).
Organising the display list
A great feature of AS3 is the display list. As we go along, we may want to add new objects to the physics engine, or bring up a pop up menu mid game. In order to make this all run smoothly, so that the right objects are always on top of each other, we are going to create blank Sprites for each of our layers now. Then when we add objects throughout the game, we can just add them to the right Sprite.
Within the class definition, before any functions, we create our layers and instantiate our Intro object (just a movieclip in the library with limited code inside)
1 2 3 4 5 6 7 8 9 | // Sprite containing the background objects private var bgObjects:Sprite = new Sprite(); // Sprite containing the physics world and graphics private var m_sprite:Sprite = new Sprite(); // Sprite containing the gameShell private var gameShell:Sprite = new Sprite(); //The game introduction private var intro:MovieClip = new Intro(); |
So basically you can think of the first three Sprites as being a bit like photoshop layers or groups. Alone they will have no visual presence, but instead will have other sprites added to them. The last object is an instance of a movieclip in the Flash library and has a timeline, a button and a tiny bit of code.
1 2 3 4 5 6 7 8 | public function TileGame() { //... other code for the came addChild(bgObjects); addChild(m_sprite); addChild(gameShell); } |
This is just a part of the instantiating function of our main class and demonstrates each of our empty Sprites being added to the display list. The last to be added will be considered to be on top of the one before. In theory, we may want to leave gameShell only to the interface of the game and make another “layer” for menus, but we can always do that later on if we feel things become complex enough to justify it.
1 2 3 4 5 6 7 8 9 10 11 12 13 | private function setup(e:Event=null):void { //Intro is added and setup gameShell.addChild(intro); intro.addEventListener(Event.ENTER_FRAME, findEnd); //Background is added and setup var background1:Sprite = new Background(); background1.y = 100; background1.x = 150; background1.blendMode = BlendMode.MULTIPLY; bgObjects.addChild(background1); } |
Above, not only can you see our intro being added to the gameShell layer, but you can also see a Background MovieClip being instantiated and positioned within the bgObjects layer. Also take note of the event listener we added to the intro, as you will see next, this is designed to detect when the intro ends.
1 2 3 4 5 6 7 8 9 | private function findEnd(e:Event=null):void { if (intro.currentFrame == intro.totalFrames) { intro.removeEventListener(Event.ENTER_FRAME, findEnd); gameShell.removeChild(intro); loadSettings(); } } |
By simply detecting the end of the intro MovieClip, we make dealing with the Intro much cleaner. We don’t have to pass variables back and forth between our main class and this imported MovieClip. We can do whatever we want in the Intro, and when we are done, just let the end of the clip play out. This function above will detect when the timeline ends, remove the intro, and continue the program.
With this system in place, making our button work in the Intro MovieClip is as easy as putting a stop() command in before the end of the timeline, and a play() command on the button.







