Empecé a desarrollar una "nave espacial cometas vs" estilo de juego de la semana pasada y ahora he llegado a una parada.
El objetivo del juego es disparar a los cometas antes de que pasen su barco. Haces que los cometas explotan al disparar contra ellos. idea simple!
Sin embargo, a veces cuando juego me sale el error "IndexOutOfBounds" . Casi siempre aparece cuando no he disparado por un tiempo (El tamaño de mis tiros ArrayList es 0) y cuando a continuación, fuego y choca de que se estrelle.
Así que tengo algún tipo de error en mi código, pero realmente no puedo verlo. Ahora espero que uno de ustedes puede ver por qué este accurs y Sálvame de más "IndexOutOfBounds" errores! :)
Aquí está la parte del código que falla, incluyendo las funciones que utilizo para mover los cometas y los tiros:
JUEGO DE CLASE
if(!Game.player.getShots().isEmpty() && !comet.getComets().isEmpty()) { //Om de är tomma så ignorera
for(int x = 0; x < Game.player.getShots().size(); x++) { //Shots X
if(!comet.getComets().isEmpty() && !comet.getComets().isEmpty()) {
for(int y = 0; y < comet.getComets().size(); y++) { //Comets Y
if(comet.getComets().get(y).intersects(Game.player.getShots().get(x)) && !comet.getComets().isEmpty() && !Game.player.getShots().isEmpty()) {
//the for loop above is the line that won't compile sometimes
comet.getComets().remove(y);
Game.player.getShots().remove(x);
score++;
}
}
}
}
}
//Comet spawn timer
comet.addComets();
//Move the comets and shots!
Game.player.moveShots();
comet.moveComets();
repaint();
COMET CLASE
public ArrayList<Rectangle> getComets() {
return comets;
}
public void moveComets() {
if(!comets.isEmpty()) {
for(int x = 0; x < comets.size(); x++) {
comets.get(x).x -= cometSpeed;
}
}
}
Jugador de clase (vacunas son en esta clase)
public void fire() {
shots.add(new Rectangle(x + player.width, y + 23, shotWidth,shotHeight));
}
public ArrayList<Rectangle> getShots() {
return shots;
}
public void moveShots() {
if(!shots.isEmpty()) {
for(int x = 0; x < shots.size(); x++) {
shots.get(x).x += fireSpeed;
}
}
}
Tenga en cuenta que los cometas y los tiros son a la vez "ArrayList fuera del objeto Rectangle"
Voy a ofrecer capturas de pantalla de error y una imagen del juego a continuación!
La línea de error se marca en el código anterior, la sentencia if debe bloquearlo se estrelle (pensé).
¡Gracias por adelantado! Toda ayuda es apreciada! :)
Debe cambiar el orden en el if para evitar que se evalúe una parte de ella. Debe cambiar su estado a:
if( x < Game.player.getShots().size() && comet.getComets().get(y).intersects(Game.player.getShots().get(x))) {
Esto se debe a que están quitando un tiro y el interior de los cometas para cuando el rodaje se elimina en la siguiente iteración del cometa se arrojarán IndexOutOfBounds
como la matriz ya no tienen la imagen que está comprobando en el caso, por lo que tendrá que comprobar de nuevo para el X existente en los tiros. También puede hacerlo en el para, usted comprobar si ambas condiciones y dejar que el se cruzan, ya que sólo comprobar en el caso.
Un mejor rendimiento si sería:
if(!Game.player.getShots().isEmpty() || !comet.getComets().isEmpty()) {
//if one of them is empty, won't be intersections
for(int x = 0; x < Game.player.getShots().size(); x++) { //Shots X
for(int y = 0; y < comet.getComets().size() && x < Game.player.getShots().size(); y++) {
//Comets Y only if the shoot still available
if(comet.getComets().get(y).intersects(Game.player.getShots().get(x))) {
//the for loop above is the line that won't compile sometimes
comet.getComets().remove(y);
Game.player.getShots().remove(x);
score++;
y = 0; // if you don't set the y = 0 the next shoot (as you removed the x, getShots.get(x) would be the x + 1 shoot) will only evaluate for the comets after y, won't evaluate the firsts comets at the array.
}
}
}
}