Game Development Reference
In-Depth Information
dispatchEvent(new CustomEventScoreBoardUpdate
(CustomEventScoreBoardUpdate.UPDATE_TEXT,
Main.SCORE_BOARD_CLICKS,String(clicks)));
The
GameFrameWork
(
ScoreBoard
listens
for
class
updates
from
this
CustomEventScoreBoardUpdate
:
game.addEventListener(CustomEventScoreBoardUpdate.
UPDATE_TEXT, scoreBoardUpdateListener, false, 0, true);
The
GameFrameWork
class acts on those events with the
scoreBoardUpdateListener
function and
passes the values needed to the
ScoreBoard.update
method. The function takes the passed in
key
as a parameter and calls the
setContentText
function of the corresponding
SidebySideScoreElement
to set the new value for the content field. A detailed description of the
SideBySideSoreElement
class is next.
The SideBySideScoreElement class
SideBySideScoreElement
is what we like to call a helper class. It probably could exist on its own
and be used by other game elements, but its primary goal is to simplify the
ScoreBoard
class
structure and code. As we were writing this framework, we found a few places where we were
creating a lot of duplicated code in a class. This was and usually is a red flag that signifies a class
is trying to do too much and needs to by modified and or split into some helper classes. The other
helper class we created was the
CustomBlitButton
for the
BasicScreen
class.
Create the
SieBySideScoreElement.as
file in the framework package structure we created earlier:
/source/classes/com/efg/framework/SideBySideScoreElement.as
Here is entire code listing for this class:
package com.efg.framework
{
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;
/**
* ...
* @author Jeff Fulton
*/
public class SideBySideScoreElement extends Sprite {
private var label:TextField = new TextField();
private var content:TextField = new TextField();
private var bufferWidth:Number;
public function SideBySideScoreElement(x:Number, y:Number,
bufferWidth:Number, labelText:String,labelTextFormat:
TextFormat, labelWidth:Number, contentText:String,
contentTextFormat:TextFormat) {
this.x = x;
this.y = y;
this.bufferWidth= bufferWidth;