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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Flash</strong> <strong>MX</strong> <strong>2004</strong> <strong>Games</strong><br />

A special kind of number<br />

If you use<br />

1 var ready:Boolean = false;<br />

2 var x:Number = 10;<br />

3 trace(ready);<br />

4 ready = x>5;<br />

5 trace(ready);<br />

Listing 7.3<br />

Here we use a special kind of number, a Boolean.<br />

Booleans are named after George Boole who was born in Lincoln, England, on 2 November 1815. In<br />

1854, he published his greatest and most influential work, An Investigation in<strong>to</strong> the Laws of Thought, on<br />

Which are Founded the Mathematical Theories of Logic and Probabilities, in which he brilliantly combined<br />

algebra with logic, the foundation of <strong>to</strong>day’s digital computers.<br />

A Boolean can have only one of two values: it is either true or false. In the short Listing 7.3 we<br />

first set the Boolean variable ‘ready’ <strong>to</strong> false. Then we set a variable x <strong>to</strong> the value 10. In line 3<br />

we trace the value of ‘ready’ <strong>to</strong> the Output window. At this stage the variable has the value ‘false’.<br />

Then we assign <strong>to</strong> ‘ready’ the value (x > 5). This reads as x is greater than 5. Clearly 10 is greater<br />

than 5 so the statement is true. When we again trace ‘ready’ in line 5 it now has the value true.<br />

Booleans are used extensively in game logic and we will meet them many times in this book.<br />

Creating and using string variables<br />

A third kind of variable is a string variable. A string is simply a sequence of characters. <strong>ActionScript</strong><br />

contains lots of useful methods for combining and accessing the characters in a string. You can<br />

create a variable that describes someone’s name:<br />

var name:String = "Steve";<br />

Or you can create a variable that can s<strong>to</strong>re a sentence:<br />

var saying:String = "The early bird catches the worm";<br />

‘Example/Chapter07/Variables04.fla’ shows how you can use several of the methods of the string<br />

object. Listing 7.4 shows the code:<br />

1 var person:String = "Steve";<br />

2 var desc:String = "The winner is ";<br />

90

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

Saved successfully!

Ooh no, something went wrong!