Howto use multiple For loops and conditions nested together?

0 favourites
  • 8 posts
From the Asset Store
SynthWave Loop Pack includes 68 seamless loops, founded on 11 original melodies.
  • http://www.filedropper.com/tetris

    I link you the capX

    It's a very simple "scanner" that should destroy only full lines of sprites when met.

    Maybe i didn't get how for loops works ?

    So, i created two for loops (and a third to destroy the line when full) nested one in another.

    The first one is scanning vertically, while the second one inside scans horizontally. And this seems to work well

    as the "witness" appears everywhere to confirm. But the "count" variable seems to mess up in the process.. :

    It should be reset to 0 each new line, and count 1 each time it meets a sprite.

    And if the line is full , should reverse back to destroy all this line.

    Then continue scanning.

    In other classical code its pretty easy.

    But in C2 it seems i cannot get it to work right.

    Can someone have the kindness to look at it ?

    Lost here..

  • http://www.filedropper.com/tetris_1

    This one is cleaner. had troubles with sprite size and collision masks. Now fixed.

    It seems to count the lines properly (2) but it only erases the last sprite of each..

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • It look like a bug in C2. The third loop stop working when you set the new position of your <scan> object.

    If you call a function to destroy your sprites, it works:

  • Thank you so much !!

    But it's kind of sad C2 doesn't handle this kind of nested conditions/loops all together.

    Anyway, i'll dig in functions usage

  • It's not a bug it's the way picking works.

    Event 4 picks only the sprite overlapping the Scan object, so in the sub event 7 you'll only be checking for an overlap with that sprite. If you want to check for overlaps with all the sprites then functions work since what's picked isn't passed to the function. Another idea would be to use the "pick all" condition.

  • No, but this picture is the corrected code by phimaka, R0j0hound.

    before i had a "for loop" that should repeat 12 times to destroy each sprite on the full line. (cf my Capx upper)

    Basically it was the exact same code, except it was nested directly in the "if count = 12" condition. And C2 wouldn't seems to run it.

    Calling the function should be the "same" here, but it works

  • I was referencing event numbers in the last capx you posted. To fix it, add a "pick all spite" condition above the "for destroy" loop in the same block.

    Before that fix or even phimaka's function solution, the loop did run, only it wouldn't destroy any other sprites other than the rightmost one because it was the only one that was picked before the "destroy" loop.

  • I see... thank you for clarifying this.

    I'll try that too !

    EDIT : yes that works ! simplier than function. But i learnt to use two things today, thank you both !

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