I'm creating my first game and it's been going well so far, but then I ran into this error.
Ok so my window is 1024x768. I am getting this exception: "ArrayOutOfBoundsException: Coordinate out of bounds!"
here the method where the exception happens:
public void drawHealthBars(){
PImage purpleHealthBar = loadImage("purpleHealthBar.jpg"); //this size of this image is 1x50 pixels
PImage greenHealthBar = loadImage("greenHealthbar.jpg"); //this size of this image is 1x50 pixels
int cHealth1 = model.player[0].getCurrentHealth(); //this value is 100
int tHealth1 = model.player[0].getTotalHealth(); //this value is 100
int cHealth2 = model.player[1].getCurrentHealth(); //this value is 100
int tHealth2 = model.player[1].getTotalHealth(); //this value is 100
int numBars1, numBars2;
if(cHealth1 > tHealth1/2) numBars1 = 2;
else if (cHealth1 > 0) numBars1 = 1;
else numBars1 = 0;
if(cHealth2 > tHealth2/2) numBars2 = 2;
else if (cHealth2 > 0) numBars2 = 1;
else numBars2 = 0;
if(numBars1 == 2){
float barSize = (float)(cHealth1-tHealth1/2) / (float)(tHealth1/2)*450;
greenHealthBar.resize((int)barSize, 50);
purpleHealthBar.resize(450, 50);
image(purpleHealthBar, 25, 25);
image(greenHealthBar, 25, 25);
}
else if(numBars1 == 1){
float barSize = (float)(cHealth1) / (float)(tHealth1/2)*450;
fill(100);
rectMode(CORNER);
rect(25, 25, 450, 50);
purpleHealthBar.resize((int)barSize, 50);
image(purpleHealthBar, 25, 25);
}
if(numBars2 == 2){
float barSize = (float)(cHealth2-tHealth2/2) / (float)(tHealth2/2)*450;
greenHealthBar.resize((int)barSize, 50);
purpleHealthBar.resize(450, 50);
image(purpleHealthBar, width -475, 25);
println( width - (25+barSize));
image(greenHealthBar, width - (25 + barSize), 25); //THIS IS WHERE THE EXCEPTION OCCURS!
}
else if(numBars2 == 1){
float barSize = (float)(cHealth2) / (float)(tHealth2/2)*450;
fill(100);
rectMode(CORNER);
rect(width-475, 25, 450, 50);
purpleHealthBar.resize((int)barSize, 50);
image(purpleHealthBar, width - (25+barSize), 25);
}
textFont(font0);
fill(0);
textAlign(CENTER);
text(cHealth1+"/"+tHealth1, 250, 65);
text(cHealth2+"/"+tHealth2, width-250, 65);
}
this method gets called in the main draw() loop.
The code works fine and displays the health bars until this method gets called when the mouse is pressed:
model.decreaseHealth(10, 0);
here is the decreaseHealth method:
public void decreaseHealth(int n, int play){
player[play].currentHealth-=n;
if (player[play].currentHealth < 0)
player[play].currentHealth = 0;
}
I do not get an exception if I called this method at the same time as the other decrease health method:
model.decreaseHealth(10,1); //note the only difference is the second parameter is a 1 instead of 0
I don't know why calling the method on player[1] makes the exception not happen but it does. Also if I call model.decreaseHealth(10,1) on its own without model.decreaseHealth(10,0) the code works fine also and the healthbars update properly.
help me please!
1