#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# Sequential Problem

edited July 2015

Hi guys. I am working on a small game where clicking groups of matching bubbles has them all pop. I have encountered a problem with my method in bubble.check() due to the fact that the bubble is killed when it checks one side so if the bubble you click has neighbors on both sides it only checks one side (Goes through the try catch statements sequentially so once one matches the others don't work due to the bubble not existing). Removing the this.killBubble() from the try catches causes the sketch to crash. Does anyone see a work-around of this method?

``````//Variables for drawing bubbles
int bubbleDiameter = 25;
int bubbleOffset = bubbleDiameter/2 + 10;

//Colors set to names for readability
color blue = color(0, 204, 251);
color red = color(254, 42, 2);
color green = color(47, 252, 25);
color yellow = color(254, 236, 76);
color pink = color(250, 1, 138);

//Arrays conataining properties of each bubble
int bubbleColor[][] = new int; //Contains an int corresponding to the bubble's color
boolean bubbleHover[][] = new boolean; //When mouse is hovering above bubble this is true
Bubble bubble[][] = new Bubble;

void setup() {

size(520, 700); //Create window
colorGen();
for(int x = 0; x < 20; x++) {
for(int y = 0; y < 25; y++) {
bubble[x][y] = new Bubble(x, y);

}
}

}

void draw() {
background(192, 192, 255);

scoreBar();

for(int x = 0; x < 20; x++) {
for(int y = 0; y < 25; y++) {
bubble[x][y].drawBubble();
}
}
checkHover();

}

class Bubble {

boolean living;
int xPos;
int yPos;
int col;

//Bubble class

//Constructor for bubble
public Bubble(int x, int y) {

this.living = true; //State of bubble
this.xPos = x; //Gives the x position of the bubble in the array
this.yPos= y; //Gives the y position of the bubble in the array
this.col = bubbleColor[x][y]; //Sets color value to that in the array generated by colorGen()
}

//
void killBubble() {
this.living = false;
this.col = 5;
}

void check(boolean b) {
this.check();
this.killBubble();
}

void check() {
boolean n = false;
try {
print("0\n");
if (bubble[this.xPos+1][this.yPos].living) {
if(this.col == bubble[this.xPos+1][this.yPos].col) {
this.killBubble();
bubble[this.xPos+1][this.yPos].check(true);
}
}
print("1\n");
if (bubble[this.xPos-1][this.yPos].living) {
if(this.col == bubble[this.xPos-1][this.yPos].col) {
this.killBubble();
bubble[this.xPos-1][this.yPos].check(true);
}
}
print("2\n");
if (bubble[this.xPos][this.yPos+1].living) {
if(this.col == bubble[this.xPos][this.yPos+1].col) {
this.killBubble();
bubble[this.xPos][this.yPos+1].check(true);
}
}
print("3\n");
if (bubble[this.xPos][this.yPos-1].living) {
if(this.col == bubble[this.xPos][this.yPos-1].col) {
this.killBubble();
bubble[this.xPos][this.yPos-1].check(true);
}
}

}
catch(Exception border) {
print(border.toString());
}
}

void drawBubble() {
if(this.living) {
//Colors bubble based on color value from array
if(this.col == 0) {
fill(red);
}
else if(this.col == 1) {
fill(blue);
}
else if(this.col == 2) {
fill(green);
}
else if(this.col == 3) {
fill(yellow);
}
else if(this.col == 4) {
fill(pink);
}
//Draws bubble in grid
ellipse(bubbleOffset + xPos*bubbleDiameter, bubbleOffset + yPos*bubbleDiameter, bubbleDiameter, bubbleDiameter);
}
}
}

//Runs when mouse is clicked
void mouseClicked() {
for(int x = 0; x < 20; x++) {
for(int y = 0; y < 25; y++) {
if(bubbleHover[x][y] == true) { //Checks which bubble the mouse is over when clicked
try {
bubble[x][y].check();
}
catch(Exception e) {}
}
}
}
}

//Checks if mouseX and mouseY fall within a certain bubble's area
void checkHover() {
for(int x = 0; x < 20; x++) {
for(int y = 0; y < 25; y++) {
if(mouseX > bubbleOffset + x*bubbleDiameter - bubbleDiameter/2 && mouseX < bubbleOffset + x*bubbleDiameter + bubbleDiameter/2
&& mouseY > bubbleOffset + y*bubbleDiameter - bubbleDiameter/2 && mouseY < bubbleOffset + y*bubbleDiameter + bubbleDiameter/2) {

bubbleHover[x][y] = true; //Sets hover to true if locations intersect
}
else {
bubbleHover[x][y] = false; //Sets hover to false if there is no intersection
}
}
}
}

//Generates random colors and assigns them to positions in an array
void colorGen() {
for(int x = 0; x < 20; x++) {
for(int y = 0; y < 25; y++) {
bubbleColor[x][y] = floor(random(5));
}
}
}

//Creates score box
void scoreBar() {
textSize(32);
fill(pink);
text("Score:", 10, 675);
text("Time:", width/2, 675);
}
``````
Tagged: