I have a problem here. I made a code so, when the player gets hit by a ball, it will show a game over screen. I have multiple balls which show up when it gets shot by a bullet. The problem is, that when I shoot a bullet, it will get removed from the world, but then, since I have a game over method, it will still look for the ball and give an error saying, Actor does not exist in world.
How can I make it so, the method only gets called, when the Ball exists?
I tried something like this but it does not seem to work.
If anyone knows, how I can fix this easily, please help out, thanks!!
public void hitball()
{
if ( ! getWorld().getObjects(ball.class).isEmpty()) {
Actor ball = getOneIntersectingObject(ball.class);
if (ball != null)
{
World myWorld = getWorld();
GameOver gameover = new GameOver();
myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
myWorld.removeObject(this);
}
} else {
return;
}
}
public void hitball2()
{
if ( ! getWorld().getObjects(ball2.class).isEmpty()) {
Actor ball2 = getOneIntersectingObject(ball2.class);
if (ball2 != null)
{
World myWorld = getWorld();
GameOver gameover = new GameOver();
myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
myWorld.removeObject(this);
}
} else {
return;
}
}
public void hitball4()
{
if ( ! getWorld().getObjects(ball4.class).isEmpty()) {
Actor ball4 = getOneIntersectingObject(ball4.class);
if (ball4 != null)
{
World myWorld = getWorld();
GameOver gameover = new GameOver();
myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
myWorld.removeObject(this);
}
} else {
return;
}
}
public void hitball8()
{
if ( ! getWorld().getObjects(ball8.class).isEmpty()) {
Actor ball8 = getOneIntersectingObject(ball8.class);
if (ball8 != null)
{
World myWorld = getWorld();
GameOver gameover = new GameOver();
myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
myWorld.removeObject(this);
}
} else {
return;
}
}
