Game Development Reference
In-Depth Information
To finish off the platforms, let's create some ground for the cat to walk on. For the Bug Catcher game,
I decided to make the ground a shallow pond:
1. Use the Rectangle tool to draw a 10-pixel-high blue rectangle across the bottom of the stage.
You might want to put it on a layer behind the foreground objects.
2. Above this blue rectangle, create a blue, 20-pixel-high drawing of waves. Convert it to a Movie
Clip symbol and give it an alpha of 40 in the Color Effect section of the Properties panel. (You'll
find a symbol called S]pan in the >qc?]p_danOui^kho
folder in the Library if you don't feel like making the
waves yourself.) I gave it the instance name s]pan.
3. Test the project and try out the new ground you've cre-
ated. Figure 9-14 shows what this might look like.
A little later in the project, you'll use some code to make the
transparent waves appear in front of the cat, which will make
the cat look like it's actually sitting in the pond.
Figure 9-14. Some ground for the cat
to walk on
Using for loops
If you're like me, you probably found that writing out or copying/pasting those ten lines of repetitive
code a terrible chore. Aren't computers supposed to be miraculous time-saving devices designed to
spare you this sort of drudge work? Yes they are, and yes there is a better way.
Those ten directives were exactly the same in every way, except for one thing: the number of the plat-
form instance. Could you make some kind of basic template of the directive and tell AS3.0 to repeat
it ten times, just inserting the correct number? Yes, you guessed it, you can! It's a programming device
called a loop .
Loops are used to repeat a section of code a specific number of times. There are quite a few different
kinds of loops you can create in AS3.0, and even though they all do almost the same thing, some are
slightly more appropriate in different situations than others. Far and away the most commonly used
loop is the bkn loop, which is a block statement that begins with the keyword bkn (meaning for this
many number of times). Any directives inside the bkn loop are repeated as many times as the loop
specifies—from once to hundreds or thousands of times.
The structure of the bkn loop might look weird and confusing at first because its arguments actually
contain three separate statements:
A variable that's used to track the number of times the loop has repeated. This is known as the
loop index variable , which is usually represented by the letter i (it stands for index ).
A conditional statement that tells the loop when it should stop.
A statement that adds 1 to the index variable every time the bkn loop repeats. (Although 1 is
usually added, you can add or subtract numbers in many different ways to fine-tune the loop
if you need to.) Each of these statements is separated by a semicolon.
Search Nedrilad ::

Custom Search