Copy the code first thing to note currentState property to join. As mentioned before, currentState attribute defines the current state of the program (state). We will currentState is set to MainMenu, said the program is started by the MainMenu state.
We also joined the mx: States elements. This element defines a program that contains all the sub-state. Here we define two states MainMenu and Game. In the MainMenu state, the end user will see the start screen. The Game states that the game itself.
The two mx: State all have name attributes. Change the currentState property value, we can switch between the completion status. Game state includes enterState and exitState two events. We can use to add two events for this response function approach to the manual "synchronize" the internal logic of the game. You see, we use EnterGame function to start the GameObjectManager (after that) instance, and set the internal flag inGame is true. This flag is used in the game rendering loop, indicating the need to render the game screen to screen. ExitGame method is simple and will inGame flag is set to false, the user interface is not visible.
Remember I mentioned that Flex in a variety of state can be switched? MainMenu state shows its simple implementation. mx: AddChild node is used to add graphical interface elements. Here, we add a button so that users can click into the game. When we left the MainMenu state, Flex will automatically remove the button without any other code and settings.
In order to render the screen to the screen, we add a mx: Canvas element. canvas (more specifically, its graphics property) acts as a double-buffered rendering process in the pre-buffering role. Rear buffer exists in the GameObjectManager class. In the enterFrame function, we call GameObjectManager the enterFrame method, which allows the screen to draw to the rear buffer. When a drawing is completed, we use the canvas in the graphics object clear, beginBitmapFill, drawRect and endFill method to draw to the canvas.
import mx.core .*;
import mx.collections .*;
import flash.display .*;
public class GameObjectManager
/ / Double buffer
public var backBuffer: BitmapData;
/ / Colour to use to clear backbuffer with
public var clearColor: uint = 0xFF0043AB;
/ / / Static instance
protected static var instance: GameObjectManager = null;
/ / The last frame time
protected var lastFrame: Date;
static public function get Instance (): GameObjectManager
if (instance == null)
instance = new GameObjectManager ();
public function GameObjectManager ()
if (instance! = null)
throw new Error ("Only one Singleton instance should be instantiated");
backBuffer = new BitmapData (Application.application.width, Application.application.height, false);
public function startup (): void
lastFrame = new Date ();
public function shutdown (): void
public function enterFrame (): void
/ / Calculate the time since the last frame
var thisFrame: Date = new Date ();
var seconds: Number = (thisFrame.getTime () - lastFrame.getTime ()) / 1000.0;
lastFrame = thisFrame;
protected function drawObjects (): void
backBuffer.fillRect (backBuffer.rect, clearColor);
Copy the code GameObjectManager objects bear the responsibility for managing various elements of the game, including enemies, players and various background elements. It also determines which elements in the rear buffer needs to draw. If you remember the pre-buffer is implemented by the canvas element, the most simple way is to canvas as a child node directly into the Application. BitmapData object from the rear to achieve the buffer, so that we can quickly direct the operation of pixels to draw the final image.
clearColor property that is used to color the scene before the establishment of a good, clear color of the rear buffer. Eventually the whole post-game element of the buffer will be overwritten, making clearColor completely unrelated, but now it is very important, because it will create a final frame. 0xFF0043AB representative of dark blue. Alpha value of the first two representatives, after the six representatives of RGB values.
Static instance property is used to implement the singleton pattern. Our program requires only a GameObjectManager instance, refer to this static property will ensure that we will only create a GameObjectManager instance. The Singleton pattern is very common programming paradigm, although Actionscript does not support protected constructor, but the singleton pattern is still very useful (once you see the instance attributes, this class is probably designed in accordance with the singleton pattern).
lastFrame property stores the last frame rendering time of completion. Track this time, we know that the last frame in the time interval between the current frame, this allows us to update the game elements. Although we do not have any game elements, in the enterFrame method, we calculated the time between frames in the frame. lastFrame startup time is when the call is reset. This is because when the whole state of the game when not in Game state, GameObjectManager not updated. If we do not reset the lastFrame, the first frame to the next level will be equal players in the off time spent between the menu. Best to avoid the termination of the player out in the first frame.
Therefore, we done? In order to achieve a variety of state, we created a Main Menu screen, use the button to enter the game. Game, we achieved double-buffering model, you can draw a blue background. Next, we will start to do interesting things, to draw something on the screen.