Game Development Reference
In-Depth Information
2. Save the I]ej*]o file and test the program. The game will now tell you whether your guess is
too high, too low, or correct (see Figure 5-13).
Figure 5-13. With an if/else statement, the game knows
whether the player's guess is correct.
After you declared the _qnnajpCqaoo variable, you then added an event listener to the cqaoo>qppkj
object and created a new event handler called kjCqaoo>qppkj?he_g that runs when the button is
clicked. It's in that event handler where all the action is, so let's a have a look at its directives in more
detail.
This is the first one:
_qnnajpCqaoo9qejp$ejlqp*patp%7
What's all that about? It copies the text from the input field in to the _qnnajpCqaoo variable. But that's
not all; it also converts the input data from plain text into a number.
When you enter text into an input text field, that text is stored in the field's built-in patp variable as
a string. Strings are words or letters, and you can usually spot them because they'll be surrounded by
quotes. Even if you type in numbers, those numbers are interpreted as characters in a line of text, not
something that can be processed mathematically. As far as strings are concerned, “789” is just another
word like “bike” or “elephant” . That's bit of a problem because the game depends on the player enter-
ing numbers that can actually be understood as numbers.
What you need to do then is convert the data from the input field's patp variable from a string to
a number. Because the numbers from 1 to 100 are all positive and don't contain decimal values, it
makes sense that they should be interpreted as data with a qejp type.
It's very easy to convert data types in AS3.0. All you need to do is surround the data you want to con-
vert in parentheses and then add the name of the type you want to convert it to. That's all that this
bit of code in bold does:
_qnnajpCqaoo9 qejp$ejlqp*patp% 7
It converts the string from the input field's patp variable into a number of the qjep type. This process
of converting one type of data to another is called casting . You'll be encountering casting a lot over
the course of the topic. It's often necessary for you to cast data or objects as a certain type to encour-
age AS3.0's compiler to run the code correctly.
 
Search Nedrilad ::




Custom Search