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.

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

//Functionality is control inside the Rhino Movie Clip<br />

//The variable ’wrongcount’ is update everytime the child<br />

//enters an incorrect value. This is handled using the ’enterData’ loop<br />

//When the child presses the return key the Clip jumps <strong>to</strong><br />

//’validateData’. A correct answer jumps <strong>to</strong> ’correct’<br />

//An incorrect answer and the Clip jumps <strong>to</strong> ’inCorrect’<br />

//where the variable ’wrongcount’ is incremented. If ’wrongcount’<br />

//is equal <strong>to</strong> one then the clip jumps <strong>to</strong> ’showAnswer’<br />

//where the correct answer is displayed for the child.<br />

//Then the game moves on <strong>to</strong> the next problem by calling ’nextProblem’.<br />

Believe me, if you do that you will feel a warm glow when you return <strong>to</strong> your own code, and<br />

other developers who read your code will think just one thing – respect!<br />

One final tip: don’t change a variable’s value outside the current scope. If you find that you<br />

do need <strong>to</strong> change the value of a variable in several places then set the variable’s value using a<br />

function, such as<br />

function setMyVariable(newValue, calledFrom){<br />

trace("setMyVariable <strong>to</strong> " + newValue + " called from " + calledFrom);<br />

myVariable = newValue;<br />

}<br />

This will make tracking down any errors much easier. The ‘trace’ will show you who attempted<br />

<strong>to</strong> change the value of the variable. Often you can find that as you develop your programs there<br />

may be errant code that is left over from an earlier incarnation of the program and that this errant<br />

code is setting the values in a movie clip inappropriately. These type of errors are much easier <strong>to</strong><br />

find if you follow the simple rule ‘don’t change the value of a variable directly outside the current<br />

scope. Always use a function call that says who was trying <strong>to</strong> change the value.’<br />

Creating a new object<br />

Object-oriented programming involves combining the data and the operations on the data within<br />

a single object. <strong>Flash</strong> allows you <strong>to</strong> create such an object. In this code snippet we create a new<br />

object by calling the ‘point’ function. This type of function is sometimes called a construc<strong>to</strong>r.<br />

Inside the function variables are created and initialized. The variables ‘x’ and ‘y’ are both set <strong>to</strong><br />

zero, but the variables ‘init’ and ‘sum’ are set <strong>to</strong> point <strong>to</strong> functions. We can use the functions<br />

from within the object. In this example ‘pt1’ calls the ‘init’ method which sets the values of ‘x’<br />

and ‘y’ <strong>to</strong> 3 and 10 respectively and ‘pt2’ uses the same method <strong>to</strong> initialize the values. Then the<br />

function ‘pt2’ uses the method ‘sum’ <strong>to</strong> add the point ‘pt1’ <strong>to</strong> its own internal data. After running<br />

the program the output window will display 23 40.<br />

1 pt1 = new point();<br />

2 pt2 = new point();<br />

3 pt1.init(3, 10);<br />

144

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

Saved successfully!

Ooh no, something went wrong!