Howto use multiple For loops and conditions nested together?

Get help using Construct 2

Post » Mon Nov 09, 2015 11:12 pm

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.. :(
twitch.tv/neozoom/
Dev stream from time to time !
@FireCamp_games <-- twitter
B
6
S
2
G
2
Posts: 52
Reputation: 969

Post » Mon Nov 09, 2015 11:40 pm

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..
twitch.tv/neozoom/
Dev stream from time to time !
@FireCamp_games <-- twitter
B
6
S
2
G
2
Posts: 52
Reputation: 969

Post » Tue Nov 10, 2015 1:29 am

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:

Image
B
15
S
6
Posts: 105
Reputation: 1,356

Post » Tue Nov 10, 2015 4:59 am

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 :)
twitch.tv/neozoom/
Dev stream from time to time !
@FireCamp_games <-- twitter
B
6
S
2
G
2
Posts: 52
Reputation: 969

Post » Tue Nov 10, 2015 6:01 am

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.
B
91
S
31
G
102
Posts: 5,233
Reputation: 67,252

Post » Tue Nov 10, 2015 6:15 am

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 :o
twitch.tv/neozoom/
Dev stream from time to time !
@FireCamp_games <-- twitter
B
6
S
2
G
2
Posts: 52
Reputation: 969

Post » Tue Nov 10, 2015 6:38 am

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.
B
91
S
31
G
102
Posts: 5,233
Reputation: 67,252

Post » Tue Nov 10, 2015 6:43 am

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 !
twitch.tv/neozoom/
Dev stream from time to time !
@FireCamp_games <-- twitter
B
6
S
2
G
2
Posts: 52
Reputation: 969


Return to How do I....?

Who is online

Users browsing this forum: Google [Bot] and 7 guests