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 />

But we want the bat <strong>to</strong> speed up as it moves. So we use another variable, this time we have called<br />

the variable ‘moveY’. It could have been called ‘threeBlindMice’ but ‘moveY’ describes what the<br />

variable is used for and this is very useful when examining your own and other developers’ code.<br />

So what are we testing? If ‘moveY’ is greater than or equal <strong>to</strong> 0 then we do the bit in the first<br />

curly brackets. The symbol ‘>’ means greater than and obviously the symbol ‘=’ means equals.<br />

So ‘>=’ means greater than or equal <strong>to</strong>. If ‘moveY’ was equal <strong>to</strong> 0 then the bat was previously<br />

stationary, if ‘moveY’ was greater than 0 then previously the bat was moving down the screen. In<br />

either case we need <strong>to</strong> set the movement of the clip so that it is going <strong>to</strong> start moving up, which<br />

we will do by setting ‘moveY’ <strong>to</strong> −1.<br />

A movie clip’s location is set using the attributes _x and _y. If_x = 100 and _y = 100, then moving<br />

the clip <strong>to</strong> (25, 80) would involve setting _x <strong>to</strong> 25 and _y <strong>to</strong> 80. This would have the effect of moving<br />

the clip <strong>to</strong> the left (by reducing the _x value) and moving the clip up (by reducing the _y value).<br />

Similarly if the clip were moved right and down then the _x and _y values would both increase.<br />

If ‘moveY’ is already less than 0, then it must already be moving up so we want <strong>to</strong> speed up<br />

its motion as long as it is not already going <strong>to</strong>o fast. In this instance we test the speed by using,<br />

yes, you guessed it, an ‘if’ statement. If ‘moveY’ is greater than −15 then we can speed up the<br />

motion by multiplying the existing motion by 1.2. If the speed was initially −1, then the speed<br />

after multiplying by 1.2 will be −1.2. Don’t worry, the maths doesn’t get much harder than that!!<br />

After we’ve done all that we can set ‘keypressed’ <strong>to</strong> true, because we know that the K key is pressed<br />

and we can move the bat by adding the moveY value on<strong>to</strong> the ‘_y’ attribute for this clip. When<br />

you are adding, subtracting, multiplying or dividing in <strong>Flash</strong>, you can use one of two methods.<br />

x = x+3; or x+ = 3;<br />

If x were initially 6, for example, then after either of the above x would be set <strong>to</strong> 9. I prefer the<br />

second option, simply because if x does equal 6 then it patently doesn’t equal 9, so the use of the<br />

equals symbol can be confusing; what it means is assign the new value of x <strong>to</strong> be the old value<br />

plus 3. The symbol ‘+=’ does not have a simple arithmetic alternative and so there is no confusion.<br />

If you prefer the first method then by all means use it; code is like that – you will get in<strong>to</strong> ways<br />

of doing things that you will feel comfortable with and if it works for you and the method works<br />

then use it. But do remember <strong>to</strong> comment any code; I cannot stress this <strong>to</strong>o firmly.<br />

The next section of the code repeats all the same tests with the ‘M’ key. This time we test<br />

for a limit of movement down the screen by ensuring that ‘_y’ is less than 270. When setting<br />

‘moveY’ we initially set it <strong>to</strong> 1 rather than −1, because we are moving the bat down the screen<br />

and ‘_y’ increases in value down. After testing for the ‘M’ key we finally check whether the variable<br />

‘keypressed’, which was initially set <strong>to</strong> false, has been set <strong>to</strong> true. The ‘!’ symbol in front of a true<br />

or false variable reverses the value of the variable. In English<br />

10<br />

if (!keypressed) moveY = 0;

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

Saved successfully!

Ooh no, something went wrong!