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 1: Your first game<br />

is true or not. <strong>Flash</strong> lets you s<strong>to</strong>re all this information. A variable takes a name, which can be any<br />

name that you find useful. In this instance I chose the name ‘keypressed’ and I have set this <strong>to</strong><br />

false. So at this stage in this section of code the variable ‘keypressed’ is false.<br />

Then we move on <strong>to</strong> use a most important <strong>to</strong>ol in the programmer’s armoury, the ‘if’ statement.<br />

The syntax of an ‘if’ statement is<br />

if (test){<br />

//Do this if test is true<br />

}else{<br />

//Do this if test is false<br />

}<br />

The standard brackets contain the test. What could we use here? It can be anything that evaluates<br />

<strong>to</strong> true or false. In Chapter 8 we will look in detail at how <strong>to</strong> use the ‘if’ statement but for now just<br />

remember true or false. If something is true then do whatever is in the first set of curly brackets. If<br />

it is false then do whatever is in curly brackets following the optional ‘else’ statement.<br />

We are using the clip event <strong>to</strong> move the bat up or down. It will move up if the ‘K’ key is<br />

pressed and down if the ‘M’ key is pressed. In <strong>Flash</strong> we can test whether a key is pressed using<br />

Key.isDown(75)<br />

The number in the brackets selects the key; you will learn how <strong>to</strong> find out a key’s number later in<br />

the section on debugging. If the key is down then the result is true, if the key is not down then the<br />

result is false. In the ‘if’ statement test we look for two things, one is the whether a key is down<br />

and the other is the value of ‘_y’. This value is s<strong>to</strong>res the position up and down the screen of the<br />

movie clip itself. To avoid it going <strong>to</strong>o high we want <strong>to</strong> make sure that this value is always greater<br />

than 50. We can combine these two tests by using the opera<strong>to</strong>r ‘&&’. Now both the tests must<br />

be true for the combination of the tests <strong>to</strong> evaluate <strong>to</strong> true. This is called logical ‘And’. Again later<br />

we will look in<strong>to</strong> combining logical operations, for now just accept that the test decides whether<br />

the K key is pressed and whether the movie clip is not <strong>to</strong>o high up the screen. If all this is true<br />

then we move on <strong>to</strong> the code within the curly brackets.<br />

if (moveY>=0){<br />

moveY = -1;<br />

}else if (moveY>-15){<br />

moveY *= 1.2;<br />

}<br />

keypressed = true;<br />

_y += moveY;<br />

What, another ‘if’ statement? Yes, programming often involves the use of many conditions.<br />

Here we know that the user is pressing the K key and that the bat is not yet in the <strong>to</strong>p position.<br />

9

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

Saved successfully!

Ooh no, something went wrong!