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

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

    [attachment=0:2k47f37s][/attachment:2k47f37s]

    So the pseudo code should be something like this:

    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
    }
    [/code:2k47f37s]
    
    So what I am asking is help to develop this idea since recursion always makes my head want to explode 
    I hope you get the rough idea! 
    
    Cheers!
    Theo
    
    =======
    [i]bubble_forum_capx.capx[/i] contains the recursive function (isn't working).
    And[i] game2.capx[/i] contains a working project without recursive function to understand what I want to achieve.
  • 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.

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

    while (nextOne.color = searchedColor{
                           pop();
    }[/code:433r0182]
  • Try Construct 3

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

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

  • [quote:2w609wuw]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!

    [quote:2w609wuw]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.

    [quote:2w609wuw]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!

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

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

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

    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?

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

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