Hello all, thanks for taking the time to help me out.
I’m trying to make a top down space shooter where you shoot asteroids as you fly through space.
I’m using a similar method as in the LibGDX tutorial for making the ‘Drop’ game for generating asteroids and having them fall down the screen, as well as adding the asteroids to an ArrayList to keep track of all of them as well as remove them if necessary.
I’m using that same method for keeping track of the shots that the player fires.
The problem isn’t in when the asteroids collide with the player, but having the asteroids collide with the bullets.
This is the first part of the approach that I’m taking to this, the iterator: my problem is that I’m unable to say
if(asteroid.overlaps(shot)) { iter.remove(); }
because the shots and asteroids are declared locally in their spawn methods…
Iterator<Rectangle> iter = asteroids.iterator();
while(iter.hasNext()) {
Rectangle asteroid = iter.next();
asteroid.y -= 200 * Gdx.graphics.getDeltaTime();
if(asteroid.y < -64) {
iter.remove();
}
if(asteroid.overlaps(playerShip)) {
//
iter.remove();
}
}
Iterator<Rectangle> shotIter = shots.iterator();
while(shotIter.hasNext()) {
Rectangle shot = shotIter.next();
shot.y += 500 * Gdx.graphics.getDeltaTime();
if(shot.y > 600) {
shotIter.remove();
}
}
public void fire() {
Rectangle shot = new Rectangle();
shot.x = playerShip.x + 26;
shot.y = playerShip.y + 32;
shot.width = 32;
shot.height = 32;
shots.add(shot);
lastFired = TimeUtils.nanoTime();
}
public void spawnAsteroid() {
Rectangle asteroid = new Rectangle();
asteroid.x = MathUtils.random(600 - 64);
asteroid.y = 320;
asteroid.width = 64;
asteroid.height = 64;
asteroids.add(asteroid);
lastAsteroid = TimeUtils.nanoTime();
}
Are there any suggestions or should is there another beginner-friendly way to manage collisions like this?
I know it’s messy but I’m very new to Java game development (but not to Java itself)