02.02.2013 Views

Flash MX 2004 Games : Art to ActionScript

Flash MX 2004 Games : Art to ActionScript

Flash MX 2004 Games : Art to ActionScript

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 7: So what is a variable?<br />

This would create a variable called ‘bigfeet’ and set its current value <strong>to</strong> 14. Used in this way the<br />

variable ‘bigfeet’ would exist throughout the life of the movie. An alternative is <strong>to</strong> declare the<br />

variable:<br />

var bigfeet = 14;<br />

Used in this way as soon as the script exits the value of ‘bigfeet’ is deleted. <strong>Flash</strong> <strong>MX</strong> <strong>2004</strong><br />

has introduced a new concept for <strong>ActionScript</strong> and one that you are encouraged <strong>to</strong> use, strict<br />

data typing. Strict data typing has many benefits; often a hard-<strong>to</strong>-find problem with a game can<br />

result in confusion over the type of a variable. If you think your variable is a number and <strong>Flash</strong><br />

thinks it is text then testing <strong>to</strong> see if it is greater than a certain value will often fail. But if you<br />

use strict data typing then <strong>Flash</strong> will inform you if you use the variable in a way that it is not<br />

expecting.<br />

1 var bigfeet:Number = 14;<br />

2 //Try assigning a string <strong>to</strong> it<br />

3 bigfeet = "Very big feet";<br />

Listing 7.1<br />

Listing 7.1 shows how <strong>to</strong> use strict data typing. The variable is declared in line 1 using the ‘var’<br />

keyword then the variable name followed by a colon and then the data type, in this case a number.<br />

Now <strong>Flash</strong> knows that this variable is <strong>to</strong> be used as a number. The next line starts with ‘//’ which<br />

<strong>Flash</strong> interprets <strong>to</strong> mean that the text that follows is ignored because it is a comment. In line 3<br />

the variable assigned a section of text, which in programming is called a string. <strong>Flash</strong> knows that a<br />

string is not a number and when you run the movie it displays the error shown Figure 7.6 in the<br />

Output window.<br />

Figure 7.6 Error message displayed after using Listing 7.1<br />

87

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!