[solved] How do I call recursive function to destroy...

Get help using Construct 2

Post » Tue Jul 29, 2014 12:43 pm

Hey folks,

as I was taking a break from my main project, I unfortunately stumbled upon a problem in my new project.
You will see two streams of bubbles facing each other from different directions (top & bottom) and as they collide they are destroyed. If two bubbles share the same color you will receive a point. By clicking on a bubble you will destroy it and so manipulate the streams. However I want to check if there are more than 2 bubbles of the same color in a row (like in every bubble shooter sort of game). If so they all should also be destroyed and some extra points shall be given.

g3029.png


So the pseudo code should be something like this:

Code: Select all
int score = 0;
if (bubbles collide){
                if (colors of bubbles match){
                    score = score + check_upper_Neighbours() + check_lower_Neighbours();
                }
}
/* same with lower_Neighbours */
check_upper_Neighbours(){
      temp_score = 0;
      if (color of bubble above matches){
           temp_score +=5;
           check_upper_Neighbours();
     }
     return temp_score
}


So what I am asking is help to develop this idea since recursion always makes my head want to explode :D
I hope you get the rough idea!


Cheers!
Theo

=======
bubble_forum_capx.capx contains the recursive function (isn't working).
And game2.capx contains a working project without recursive function to understand what I want to achieve.
You do not have the required permissions to view the files attached to this post.
Last edited by Gougth on Tue Jul 29, 2014 5:31 pm, edited 1 time in total.
B
14
S
2
G
1
Posts: 53
Reputation: 850

Post » Tue Jul 29, 2014 1:07 pm

So you mean like the bejeweled/candycrush type 3 in a row thing?. You want to check if the bubble that is also 2 places ahead in a row has the same color as well, instead of just the one next to it( i.e , just one place ahead)?

I can't open your capxs as my C2 version is older and my intenet is too shitty to download the new versions now.
B
16
S
4
Posts: 142
Reputation: 1,462

Post » Tue Jul 29, 2014 1:32 pm

@Pandy

no not exactly like that. It should be checked in two directions. The bubble above the upper one (who had collision) should be checked. If it matches the color then the next one above should be checked and so on. Like a "pop function" on a stack. Same goes for the lower ones (lower than the lower bubble which has collided)

Code: Select all
while (nextOne.color = searchedColor{
                       pop();
}
You do not have the required permissions to view the files attached to this post.
B
14
S
2
G
1
Posts: 53
Reputation: 850

Post » Tue Jul 29, 2014 2:14 pm

Oh, bubble shooter. So if the number of bubbles in a row that has the same color is 3 or more, it has to be destroyed. Am i right?

And you want it to check if the color is the same just for a certain row, in both direction from a bubble, instead of all the rows. right?

Now i'm just wondering what you mean by recursive function. You mean a function that has to check for all the colors?
B
16
S
4
Posts: 142
Reputation: 1,462

Post » Tue Jul 29, 2014 2:24 pm

Oh, bubble shooter. So if the number of bubbles in a row that has the same color is 3 or more, it has to be destroyed. Am i right?

they are destroyed if 2 of the same color are colliding. If there are even more than those 2, all of the same color should be destroyed, yes!

And you want it to check if the color is the same just for a certain row, in both direction from a bubble, instead of all the rows. right?

yeah, we just focus on a single row for the moment. There are always 2 bubbles involved in crash since they have the same speed and start at opposite positions. Yes.

Now i'm just wondering what you mean by recursive function. You mean a function that has to check for all the colors?

I thought about a function which has to call itself again if certain criteria match. It is called to check the next one in the row. If this one matches, it calls itself again with the position of the last matching bubble again and if this also matches it calles itself again and so on.

It is so difficult to explain this without the capx! :|
B
14
S
2
G
1
Posts: 53
Reputation: 850

Post » Tue Jul 29, 2014 2:35 pm

One way to handle this could be to do a for each (ordered) to pick the bubbles in the good order (by ordering by their Y position for exemple, or their UID), then store their color inside a string formatted like "red|red|blue" saved into a variable,then you apply a while loop that will compare the value of each token of the string to the value of the sametokenposition+tokencount as long as sametokenposition+tokencount is the same value as the token itself, counting the number of time the while was used, when the while is over, you have the number of same bubbles, and you can jyst pop them out.

I am pretty sure there is a muuuch easier way of handling that, but basically you would compare the value of the bubble to the next until they are not equal anymore.
Game design is all about decomposing the core of your game so it becomes simple instructions.
B
53
S
22
G
18
Posts: 2,122
Reputation: 17,123

Post » Tue Jul 29, 2014 3:51 pm

I took your game2.capx and modified it to make it work like I think you intended.

I used two recursive functions, one for each "direction" of neighbour checking. As Aphrodite hinted, the magic come from the "For each (ordered)", which combined with an immediate "Stop loop", essentially turns it into a makeshift "Pick by ordering". Each time a color match is made, the functions are called again with new parameters, up until no match are found.
You do not have the required permissions to view the files attached to this post.
B
75
S
31
G
27
Posts: 1,000
Reputation: 20,735

Post » Tue Jul 29, 2014 5:30 pm

@Aphrodite
thanks for the idea, havn't thought about "for each" in this case! And as Magistross pointed out that was the key to success! Thanks!

@Magistross
thanks a lot mate, thats exactly what i wanted to do! I love it to see that there is still so much to learn for me. It seems so obvious and easy in your version, i couldn't have thought about it beeing that easy.

Thank you all very much, you've helped me to unravel my thougths about recursion in c2. :D


edit:

i got one question left:
1) we don't know which one is instance0 and which one is instance1, do we? Is this random or could it be always instance0 = top (while colliding) and you just coded this to be safe?
You do not have the required permissions to view the files attached to this post.
B
14
S
2
G
1
Posts: 53
Reputation: 850

Post » Tue Jul 29, 2014 6:16 pm

Haha, yeah, I did that just to be safe, although there might have been a better way. But this method was quick and easy.

We don't know for sure which instance is the collider nor which instance is the collided, it all depends on their UID (i.e. order of creation) I believe.
B
75
S
31
G
27
Posts: 1,000
Reputation: 20,735


Return to How do I....?

Who is online

Users browsing this forum: Christmas, NN81 and 12 guests