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.

9 Using<br />

loops<br />

If there is one thing that computers are good at (but if yours has just crashed then you might<br />

be forgiven for thinking they are good at precious little) it is doing boring repetitive jobs. In<br />

this chapter we will look at how you can tell your program <strong>to</strong> repeat and repeat and repeat. No<br />

programming language would be complete without the ability <strong>to</strong> repeat sections of code several<br />

times. <strong>Games</strong> regularly need <strong>to</strong> run a section of code several times. In this chapter we will look<br />

at creating sections of code that repeat and show you how <strong>to</strong> control the program flow in these<br />

situations. We will learn how <strong>to</strong> jump out of the code if a certain condition is met. We will<br />

consider using repeats that run at least once and repeats that may never run at all. One of the most<br />

important aspects of using loops is the initializing of the data used in a loop. We will look at the<br />

potential pitfalls from initialization errors.<br />

Creating sections of code that repeat<br />

<strong>Flash</strong> has several options for repeating; the first option is used when you already know how many<br />

times you want <strong>to</strong> repeat a section of code. In this circumstance you use the ‘for’ statement:<br />

sum = 0;<br />

for(i=1; i

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

Saved successfully!

Ooh no, something went wrong!