Game Development Reference
In-Depth Information
Chapter 3
Creating a Game World
This chapter shows you how to create a game world by storing information in memory. It introduces
basic types and variables and how they can be used to store or change information. Next, you see
how to store more complicated information in objects that consist of member variables and methods.
Basic Types and Variables
The previous chapters discussed memory a couple of times. You have seen how to execute a
simple instruction like canvasContext.fillStyle = "blue"; to set the color a shape should be filled
with when drawn on the canvas. In this chapter's example, you use memory to store information
temporarily, in order to remember the results of a few simple calculations. In this DiscoWorld
example, you change the background color depending on the time that has passed.
Types
Types, or data types , represent different kinds of structured information. The previous examples used
different kinds of information that were passed as parameters to functions. For example, the function
fillRect wants as information four integer numbers, the start function in the BasicGame example
wants a text identifier that refers to the canvas, and the update and draw functions from the same
example don't need any information at all. The browser/interpreter can distinguish between all these
different kinds of information and, in many cases, even convert information of one type to another.
For example, in JavaScript, you can use either single or double quotes to represent text.
For example, the following two instructions do the same:
canvas = document.getElementById("myCanvas");
canvas = document.getElementById('myCanvas');
Browsers are able to automatically convert between different kinds of information. For example, the
following would not result in a syntax error:
canvas = document.getElementById(12);
29
 
Search WWH ::




Custom Search