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]