Game Development Reference
In-Depth Information
Loops
Loops are extremely useful when it comes to performing tasks such as checking large
amounts of data or checking a function multiple times. GameMaker has four loops avail-
able for use. Let's start simple, shall we?
The repeat loop
The simplest loop in GameMaker would have to be the repeat loop. The repeat loop
does exactly what it says; it repeats.
The repeat loop will repeat any code in curly braces that follow it the number of times
defined by the programmer. Here is a screenshot of how this looks:
The code in the preceding screenshot is a bit more complex than the previous example, but
what it does is simple.
It starts by initializing a local variable named xx . It sets this variable value to 0 .
It then moves into the repeat statement. You can see number 10 in brackets to the right
of it. This means the code within the curly braces will be repeated 10 times.
The first line in the curly braces creates a new object named obj at position xx,y in the
room. Remember that xx is equal to 0 at the first run, so the final position is 0,y . y is a
built-in variable that holds the object's current y position in the room.
The next line adds 32 to the xx variable. On first run, this would make xx equal to 32 ; on
the second run xx would become 64, and so on.
The preceding code will create 10 objects in a line from left to right.
Search WWH ::




Custom Search