Game Development Reference
In-Depth Information
If you want to convert a string value to a numerical value, things get a bit more complicated. This
isn't an easy operation for the interpreter to perform, because not all strings can be converted to a
numerical value. For this, JavaScript has a few useful built-in functions. For example, this is how you
can transform a string into an integer number:
var x = parseInt("10");
If the string passed as a parameter isn't a whole number, the result of the parseInt function is the
integer portion of the number:
var y = parseInt("3.14"); // y will contain the value 3
To parse numbers with decimals, JavaScript has the parseFloat function:
y = parseFloat("3.14"); // y will contain the value 3.14
If the string doesn't contain a valid number, the result of trying to parse it using one of these two
functions is the constant NaN (not a number; see also Figure 12-1 ).
A Few Final Remarks
Congratulations—you've completed your first game! Figure 12-3 contains a screenshot of the final
game. While developing this game, you've learned about a lot of important concepts. In the next
game, you expand on the work you've already done. In the meantime, don't forget to play the game!
You'll notice that it becomes really difficult after a few minutes, because the paint cans come down
faster and faster.
Figure 12-3. Screenshot of the final version of Painter
 
Search WWH ::




Custom Search