Game Development Reference
In-Depth Information
be changed outside the class they're a member of. For example, here is the modified constructor of
the Canvas2D_Singleton class that follows this rule:
function Canvas2D_Singleton() {
this._canvas = null;
this._canvasContext = null;
}
You also add a new method to the class, drawText , which can be used to draw text on the screen at
a certain position. The drawText method is very similar to the drawImage method. In both cases, you
use the canvas context to perform a transformation before drawing the text. This allows you to draw
text at any desired position on the canvas. Furthermore, you can change the color of the text and the
text alignment (left, center, or right). Look at the Painter10 example belonging to this chapter to see
the body of this method.
Drawing text on the screen is now easy using this method. For example, this draws some green text
at upper left on the screen:
Canvas2D.drawText("Hello, how are you doing?", Vector2.zero, Color.green);
Characters and Strings
A sequence of characters is called a string in most programming languages, including JavaScript.
Just like numbers or Booleans, strings are primitive types in JavaScript. Strings are also immutable .
This means once a string is created, it can't be changed. Of course, it's still possible to replace the
string with another string. For example:
var name = "Patrick";
name = "Arjan";
In JavaScript, strings are delimited by single or double quote characters. If you start a string with a
double quote, it should end with a double quote. So, this isn't allowed:
var country = 'The Netherlands";
When you assign a string to a variable, the string is called a constant . In addition to string values,
constant values can be numbers, Boolean values, undefined , or null , as expressed in the syntax
diagram in Figure 12-1 .
 
Search WWH ::




Custom Search