It worked!
But when I check in my Bullet class it doesn't work and gives me an error!
This error:
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: #iterator() cannot be used nested. at
com.badlogic.gdx.utils.Array$ArrayIterator.hasNext(Array.java:577) at com.myflappyoyun.game.states.PlayState.update(PlayState.java:53) at com.myflappyoyun.game.states.GameStateManager.update(GameStateManager.java:25) at com.myflappyoyun.game.FlappyDemo.render(FlappyDemo.java:36) at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:225) at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:126)
The process finished with exit code 0
public class PlayState extends state {
private static final int TUBE_SPACING = 125;
private static final int TUBE_COUNT = 4;
private static final int GROUND_Y_OFFSET = -50 ;
private Bird bird;
private Texture bg;
private Texture ground;
private Vector2 groundPos1 , groundPos2 ;
private Array<Tube> tubes ;
public PlayState(GameStateManager gsm) {
super(gsm);
bird= new Bird(50 , 300);
cam.setToOrtho(false, FlappyDemo.WIDTH/ 2, FlappyDemo.HEIGHT/ 2);
bg = new Texture("bg.png");
ground = new Texture("ground.png");
groundPos1 = new Vector2(cam.position.x - cam.viewportWidth / 2, GROUND_Y_OFFSET);
groundPos2 = new Vector2((cam.position.x - cam.viewportWidth / 2) + ground.getWidth(), GROUND_Y_OFFSET);
tubes = new Array<Tube>();
for (int i = 1; i < TUBE_COUNT ; i++) {
tubes.add(new Tube(i * (TUBE_SPACING + Tube.TUBE_WIDTH)));
}
}
@Override
protected void handleInput() {
if (Gdx.input.justTouched())
bird.jump();
}
@Override
public void update(float dt) {
handleInput();
updateGround();
bird.update(dt);
cam.position.x = bird.getPosition().x + 80 ;
for (Tube tube : tubes) {
if (cam.position.x - (cam.viewportWidth/ 2) > tube.getPosTopTube().x + tube.getTopTube().getWidth()){
tube.reposition(tube.getPosTopTube().x + ((Tube.TUBE_WIDTH + TUBE_SPACING) * TUBE_COUNT));
}
if (tube.collides(bird.getBounds()))
gsm.set(new PlayState(gsm));
}
if (bird.getPosition().y <= ground.getHeight() + GROUND_Y_OFFSET)
gsm.set(new PlayState(gsm));
cam.update();
}
@Override
public void render(SpriteBatch sb) {
sb.setProjectionMatrix(cam.combined);
sb.begin();
sb.draw(bg , cam.position.x - (cam.viewportWidth / 2), 0);
sb.draw(bird.getTexture(), bird.getPosition().x,bird.getPosition().y);
for (Tube tube : tubes) {
sb.draw(tube.getTopTube(), bird.getPosition().x, tube.getPosTopTube().y);
sb.draw(tube.getBottomTube(), tube.getPosBotTube().x, tube.getPosBotTube().y);
}
sb.draw(ground, groundPos1.x, groundPos1.y);
sb.draw(ground, groundPos2.x, groundPos2.y);
sb.end();
}
@Override
public void dispose() {
bg.dispose();
bird.dispose();
ground.dispose();
for (Tube tube : tubes)
tube.dispose();
System.out.println("Play State Disposed");
}
private void updateGround() {
if (cam.position.x - (cam.viewportWidth / 2) > groundPos1.x + ground.getWidth())
groundPos1.add(ground.getWidth()*2, 0);
if (cam.position.x - (cam.viewportWidth / 2) > groundPos2.x + ground.getWidth())
groundPos2.add(ground.getWidth()*2, 0);
}
}
PlayState.update()
method – Arctic45for (Tube tube : tubes)
withfor (int i = 1; i < TUBE_COUNT ; i++)
. – Arctic45return;
aftergsm.set(new PlayState(gsm));
to stop iterating throughtubes
after colliding. – Arctic45