Game Development Reference
In-Depth Information
// GETTER/SETTERS
public function get wordIndex():uint {
return _wordIndex;
}
public function set wordIndex(value:uint):void {
_wordIndex = value;
wordField.text = (_wordIndex) ? _wordIndex.toString() : "" ;
}
First, we declare two TextField objects that will be used to dis-
play the letter in the tile when it is entered and the number of the
word if it is the first letter. The letter property will be used to store
the letter that belongs to this square. Note that this is not the letter
that will be used to store user input, but rather the correct answer
from the XML. The acrossIndex and downIndex variables will store
the tile
s associations with any horizontal or vertical words. For the
tileIndex, or the actual grid position in the puzzle, we use a Point
object since it already has x and y properties.
'
PUBLIC PROPERTIES ARE OKAY
You don
t always have to use getters and setters for every publicly exposed
property in your classes. If a variable is purely being stored, the extra over-
head of a function to do so is unnecessary. Use getters and setters when
some other action needs to take place when a value is set, but you want
the simplicity of a simple variable assignment. As long as you keep to a
standard convention (protected/private variables always begin with an
underscore, for instance), you can easily convert the public variable to a
getter/setter property later on and cannot change more than a couple of
lines of code. Getters also come in handy when you want a value to be
readable but not writeable; simply omit the setter and you
'
'
re done!
Next, we have two protected variables, one of which has getter/set-
ter methods. The wordIndex property is used to store the word in the
puzzle (horizontal or vertical) to which the tile belongs. The _answer
property will store a player-inputted answer. We now move on to the
publicly exposed methods, which can be called from outside the class.
public function setAnswer(e:KeyboardEvent):Boolean {
if (e.keyCode
>
= String(
"
A
"
).charCodeAt(0) && e.keyCode
).charCodeAt(0)) {
_answer = String.fromCharCode(e.keyCode);
letterField.text = _answer;
return true;
} else if (e.keyCode == Keyboard.BACKSPACE) {
_answer = "" ;
letterField.text = _answer;
<
= String(
"
Z
"
}
Search Nedrilad ::




Custom Search