Bubble shooter app needs serious tweaking

Thank you beforehand if you are reading this. I am trying to design a bubble shooter app in the classical style. If at least three balls match color, all of the balls of the same color that are touching disappear. I am very close to accomplishing the goal I set out for, but the code looks bulky and inefficient. I started with 84 randomly colored balls on the stage (7 rows of twelve, 4 different possible colors). Then I put each ball into an array. Every ball has a .number property that represents its position in the array. This is so the balls can be checked for similar color while retaining the position in the array. If the ball was all the way to the left of the stage, I gave it a .left property. If the ball was all the way to the right of the stage I gave it a .right property. This was to ensure that balls on the extreme left or right of the stage would be the end of the checking process. I also gave every ball a .color property to be able to check if balls touching were the same color, and then gave every ball that matched color to the collision ball (if there were at least three of the same color) a .gone property so it could be removed later. The .left, .right, and .gone properties are all Boolean. The problem I am having is checking for the same colored balls as the collision ball. The only way I have found to do it is to nest a ton of “while” statements, checking every direction of every ball that matches color. For example…

 --Check the ball to the left (as long as it's not the last ball on the left)  
 while(ballArray[number - 1] ~= nil) and (ballArray[number].left == 0) and (ballArray[number-1].color == event.other.color) and (ballArray[number - 1].gone == 0) do   
 number = (number - 1);  
 ballArray[number].gone = 1;  
 --Check the ball up from the ball to the left (as long as it's not on the top row)  
 while(ballArray[number - 12] ~= nil) and ((number - 12) \>= 1) and (ballArray[number-12].color == event.other.color) and (ballArray[number - 12].gone == 0) do  
 number = (number - 12);  
 upCount = upCount + 12;  
 ballArray[number].gone = 1;   
 --Check the ball to the left of the ball up from the ball to the left  
 while(ballArray[number - 1] ~= nil) and (ballArray[number].left == 0) and (ballArray[number - 1].color == event.other.color) and (ballArray[number - 1].gone == 0) do  
 number = (number - 1);  
 leftCount = leftCount + 1;  
 ballArray[number].gone = 1;  
 end  
 number = number + leftCount;  
 leftCount = 0;   
 --Check the ball to the right of the ball up from the ball to the left  
 while(ballArray[number + 1] ~= nil) and (ballArray[number].right == 0) and (ballArray[number+1].color == event.other.color) and (ballArray[number + 1].gone == 0) do  
 number = (number + 1);  
 rightCount = rightCount + 1;  
 ballArray[number].gone = 1;   
 end  
 number = number - rightCount;  
 rightCount = 0;  
 end  
 number = number + upCount;  
 upCount = 0;  
 end  
  

The variables included are just so I can get back to my place in the array when I jump out of the “while” statements.

This section of code is only checking the balls to the left of the collision ball, and the embedded “while” statements are still not close to enough to accomplish what I am trying to do. The rest of the code includes checking the balls to the right, up, and down. My HUGE question is, does anyone have a better way to accomplish this? I have been picking my brain and I know it’s just a clone but it’s driving me freaking nuts. Any help would be appreciated. [import]uid: 110526 topic_id: 27854 reply_id: 327854[/import]

Consider checking out the physics samples on this web site. If you make all the balls ‘physics objects’, you can let the physics collision detection do most of the work.

If your ‘shooting’ ball collides with any ball, the physics engine will call the collision event, and in that function you will be able to check the colors of the balls involved in the collision, if they are not the same color your done checking. If they are the same, you would then check the color of balls in the array to the left and right the ball that was struck by the shooting ball.

Without knowing exactly how your game plays, that is the best I can suggest. It may take you 30 minutes or so to go thru the physics samples, but once you do, you will likely see how it may be a good way to do what you are trying to do.

Good luck [import]uid: 35148 topic_id: 27854 reply_id: 112809[/import]

Thank you ripps7 for the response. I also want you to know how appreciative I am of the fact that you are the first and only person to reply. The problem is not with the physics or collision detection though; they are both down pat. The problem lies with checking the balls after the collision. For instance, say the shooter ball collides with a same colored ball. Then let’s say there are five of those same colored balls to the left. Three balls from the collision there are five same colored balls going up. Three balls up, there is three same colored balls going right. And then three balls right there is a same colored ball going down. If you would envision it in your head, it looks like a spiral. This means that I have to check every individual ball around every ball that is the same color as the involved collision. The “while” statements are the only way I have found to do this and I was just wondering if someone had a better way. Since no one has responded with a better way, I guess the way I am already doing it is the only way to complete the task. Thank you so much for responding, and, again, I sincerely appreciate the fact that you were the only one to do so.

Dwizzl [import]uid: 110526 topic_id: 27854 reply_id: 113250[/import]

I wish you luck, and if it is working, it might be best to stick with what is working, even if it could be cleaner and more efficiently coded. From what I know of the game, it is not a high speed graphic type game, so even though you have a large number of while loops, checking on only 84 objects should not cause issues with frame rate etc…

[import]uid: 35148 topic_id: 27854 reply_id: 113351[/import]