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.

Chapter 11: Debugging<br />

would give the result 4, if num was 14 and userInput was 10, with no requirement <strong>to</strong> force userInput<br />

<strong>to</strong> be a number. <strong>Flash</strong> ‘knows’ that if you are using the ‘–’ opera<strong>to</strong>r then the value <strong>to</strong> the left and<br />

right of the operation is a number.<br />

In many languages you are forced <strong>to</strong> define what type of variable you are using, then when<br />

you combine incompatible types you are warned. <strong>Flash</strong> up <strong>to</strong> <strong>MX</strong> <strong>2004</strong> was not a strongly typed<br />

language so you have <strong>to</strong> be careful that you are combining data types in the way intended. Now<br />

that strict data typing is available you are recommended <strong>to</strong> use it.<br />

Declare your variables<br />

<strong>Flash</strong> does not force you <strong>to</strong> declare variables. This is both a benefit and a problem. See if you can<br />

work out what will happen in the following code:<br />

piece = 10;<br />

trace("The current value of piece is " + getPiece());<br />

function getPiece(){<br />

return peice;<br />

}<br />

Award yourself a prize if you thought that the value returned would be undefined. Instead of 10<br />

being the return value, the value will be empty because there is a typo error. The function returns<br />

the value of the undefined variable ‘peice’ instead of the variable ‘piece’.<br />

Errors like this are quite hard <strong>to</strong> spot because <strong>Flash</strong> creates variables as you use them. In a strongly<br />

typed language you would get a compilation error because you are returning a value that does not<br />

exist.<br />

Here’s another one:<br />

1 i=10;<br />

2 trace(sumUpTo());<br />

3 i--;<br />

4 trace(sumUpTo());<br />

5<br />

6 function sumUpTo(){<br />

7 <strong>to</strong>tal = 0;<br />

8 while (i>0){<br />

9 <strong>to</strong>tal += i;<br />

10 i--;<br />

11 }<br />

12 return <strong>to</strong>tal;<br />

13 }<br />

Listing 11.1 ‘Examples/Chapter11/debug03.fla’<br />

153

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

Saved successfully!

Ooh no, something went wrong!