Game Development Reference
In-Depth Information
public function ScoreBoard() {
init();
}
private function init():void {
textElements = {};
}
public function createTextElement(key:String,
obj:SideBySideScoreElement):void {
textElements[key] = obj;
addChild(obj);
}
public function update(key:String, value:String):void {
var tempElement:SideBySideScoreElement = textElements[key];
tempElement.setContentText(value);
}
}
}
Class import and variable definition for the ScoreBoard class
This version of the ScoreBoard class places text labels and their corresponding values next to one
another horizontally on the screen. Just as we created the SimpleBlitButton class to help create
simple sprite-like buttons to use on any instance of the BasicScreen class, we will also create a UI
element called SideBySideScoreElement as a helper class for the ScoreBoard .
The textElements object will hold an array of instances of the SideBySideScoreElement class.
Next, we define SideBySideScoreElement instances to hold our scoreboard UI objects.
The ScoreBoard is an attribute the GameFrameWork class, not of the Game class. In fact, we are
going to get a ”design pattern” oriented here and say that the ScoreBoard class and the Game
class cannot even communicate with one another except through the GameFrameWork class. We
do this to keep our classes very clean and free of extraneous references that will make future
reuse difficult. In the case of the framework, the GameFrameWork class could be said to be a type
of controller class. The Game class will stay independent from Main as much as possible. We are
decoupling the game logic completely from the other classes in the framework. We do this
essentially to be able to pull the game from the framework and use it in another framework (if
needed).
The constructor and init follow the structure that we will use for almost all custom classes (with
the exception of some helper classes). If nothing is passed into the constructor, we usually simply
call an init function to set up the custom class.
The init function simply initializes the textElements object that will hold instances of the
SideBySideScoreElement class. We will discuss the SideBySideScoreElement class in the next
section. When we get to the section of this chapter on customizing the GameFrameWork.as class
(as a file called Main.as in our game project), we call a public function in this class named
createTextElement . This creates an associative array of the textElement object instances that
we can access by the key. The key will be the String name we pass into the constructor of the
SideBySideScoreElement . For example, the String score will be used to access the
SideBySideScoreElement instance that represents the score for the user.
Search WWH ::




Custom Search