# Loops

### » Wed Nov 28, 2007 2:09 am

Hi, I'd like a little help with using loops, specifically While loops. I can't for the life of me work out how to structure it or anything... I dunno what has to be a condition or a separate event or anything.
B
3
S
2
G
5
Posts: 351
Reputation: 2,377

### » Wed Nov 28, 2007 3:55 am

lol nevermind, I figured it out
B
3
S
2
G
5
Posts: 351
Reputation: 2,377

### » Wed Nov 28, 2007 7:09 am

well, then tell me how you did it, cuz i don't get it.
B
8
S
2
G
5
Posts: 744
Reputation: 3,288

### » Wed Nov 28, 2007 9:20 am

Ok, it's a little tricky to explain, not to mention I suck at forum examples lol
For the While loops, it will basically keep looping until whatever condition is in the event along with 'while' doesn't happen anymore.

For example:

1 while
player overlaps obstacle

- change Y to player.Y - 1
(or 0.1 if you wanna be super accurate)

The loop moves the player up a pixel each time through the loop until the player is no longer overlapping the obstacle.

The other loops like 'For' are a little more interesting and I -think- they require the use of sub-events, although are harder for me to use. Basically instead of the loop repeating until a condition is met, the loop runs a predeterminded amount of times.

For example:

1 For "move" = 1 to 10
2 --(subevent)When Right Arrow is down - player.X = player.x + 1

The result is what looks like the player moving to the right in increments of 10 pixels when in fact it's moving one pixel at a time

That's all I learned from playing around with the loops in construct, I'm still working it all out though lol!
B
3
S
2
G
5
Posts: 351
Reputation: 2,377

### » Wed Nov 28, 2007 1:43 pm

Ok, i didn't really get all of that, but at least i got a clue of how it works. If you are around Ashley you could maybe give us a more simple explanation.
B
8
S
2
G
5
Posts: 744
Reputation: 3,288

### » Wed Nov 28, 2007 7:40 pm

The While and For loops are a nod at "real" programming languages. Be careful with While loops, it is possible to get stuck in an infinite loop and your game will become unresponsive. I just tested it out and I got it to freeze up pretty easily (and I had several Temp.exe's in my task manager too. I froze it before it even drew the window )

As for the For loops, in BASIC it would look like this:

[code:24u1bze4]
FOR X = 1 TO 10
Y=Y+1
NEXT
[/code:24u1bze4]

This sets up the loop to run ten times (1 TO 10). X is the loop name, but it is also a variable... each time the loop goes around, it gets 1 added to it. As for the code inside the loop, on each pass the loop adds 1 to Y. When X equals 10, the loop ends. When the loop is finished the result is Y = 10.

In Construct, if you want to make a simple loop like this that just runs a specific number of times, it's more efficient just to use the "Repeat" loop. Also, there is no "NEXT" command, Construct just automatically knows when the loop ends.

You could also nest loops like so:

[code:24u1bze4]
FOR X = 1 TO 10
FOR Y = 1 TO 5
Z=Z+1
NEXT Y
NEXT X
[/code:24u1bze4]

For each X loop, Y loops five times. The result when both loops are done is Z equals 50. In Construct, you can nest loops by using sub events:

You could also use variables to define the parameters of the loop instead of regular numbers:

[code:24u1bze4]
FOR X = 1 TO numOfEnemies

FOR X = playerHealth TO playerMaxHealth

etc.
[/code:24u1bze4]

And in Construct:

Moderator
B
5
S
2
G
6
Posts: 4,348
Reputation: 10,971

### » Sat Dec 01, 2007 8:27 pm

While keeps going until the event conditions are no longer true, so:

+ While
+ Player overlaps floor
: Set Player Y to .Y - 1

will keep moving the player up 1 pixel until it isn't over the floor any more, then the event finishes. The effect is it can never be overlapping a floor - it will always be on top of the floor. (NB. there's no point using a value less than 1: collisions are done to a resolution of 1 pixel)

Also it's worth mentioning you can use these without subevents and with conditions in the same event too eg:

+ For "i" 1 to 10
+ Player is overlapping floor ...

etc.

'For' is basically like a customisable 'Repeat'. If you have:

+ Repeat 10 times
: Player: Set Y to .Y + 1

the player moves 10 pixels down.

If you have:
+ For "i" from 1 to 10
: Player: Set Y to .Y + 1

the same thing happens. 'For' is useful if you want to have a named index (for nested loops) or run a specific range of indices (eg. from 500-1000).
Scirra Founder
B
362
S
216
G
75
Posts: 23,119
Reputation: 180,497