0 Favourites

Loops

This forum is currently in read-only mode.
  • 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.

    Any help with loops would be gladly received <img src="{SMILIES_PATH}/icon_biggrin.gif" alt=":D" title="Very Happy" />

  • lol nevermind, I figured it out <img src="{SMILIES_PATH}/icon_wink.gif" alt=";)" title="Wink" />

  • well, then tell me how you did it, cuz i don't get it.

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • 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!

    Hope that was helpful

  • 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.

  • 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 <img src="{SMILIES_PATH}/icon_razz.gif" alt=":P" title="Razz" /> )

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

    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:
    
    <img src="http://xs121.xs.to/xs121/07483/loop.jpg">
    
    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:
    
    <img src="http://xs121.xs.to/xs121/07483/loop2.jpg">
  • 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).

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)