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.

8 In<br />

tip-<strong>to</strong>p condition<br />

Should I go <strong>to</strong> Barcelona at Easter? If I have the money, there are flights available and I can<br />

get a hotel, then I’ll go. Life is full of decisions: should I take that job? Should I join a gym?<br />

Should I make a cup of tea? Whenever you make a decision, you will consider a number of<br />

options and in the end you will either decide <strong>to</strong> do something or you will choose not <strong>to</strong>. As<br />

you embark on your programming career you may be thinking, if this doesn’t get any easier then<br />

I’ll stick <strong>to</strong> producing artwork. In this chapter we look at making decisions in your programs<br />

using the marvellous ‘if ’ statement. An ‘if ’ statement takes a logical argument that resolves <strong>to</strong><br />

true or false; none of the difficult in-between concepts for a computer, it’s either true or it’s false<br />

and no half measures. Sometimes we need <strong>to</strong> combine several decisions before we come <strong>to</strong> our<br />

final conclusion. Multiple decisions are handled using Boolean logic, which is introduced in this<br />

chapter. We will illustrate some of these concepts using a keyboard-controlled walking bucket and<br />

some very attractive lights. So without further ado let us set sail on the sea of conditions.<br />

The marvellous ‘if ’ statement<br />

Because it is almost impossible <strong>to</strong> write any program without making a decision, you have already<br />

seen the ‘if ’ statement in action. The introduc<strong>to</strong>ry chapter used the ‘if ’ statement and so <strong>to</strong>o did<br />

the last chapter. You probably had a good idea what was going on, but you were not properly<br />

introduced. The marvellous ‘if ’ statement can be used in one of three different ways. The simplest<br />

way is like this:<br />

if (condition){<br />

//Do something if condition is true<br />

}<br />

The line starts using ‘if ’, and then in brackets we put a condition. This can be anything that<br />

evaluates <strong>to</strong> true or false. You can even put the keyword true in there and then the ‘if ’ statement<br />

always evaluates <strong>to</strong> true, so the code in the curly brackets is always executed. Later in this chapter<br />

we will look at the different conditions you can use.<br />

The second variant of the ‘if ’ statement takes this form:<br />

if (condition){<br />

//Do something if condition is true<br />

}else{<br />

//Do something if condition is false<br />

}<br />

101

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

Saved successfully!

Ooh no, something went wrong!