1
\$\begingroup\$

am making a simple in which you drag a player on screen and when a ball hits you you lose ,so you have to move the player away from the balls.level zero:no balls, level one:there is one ball on screen, level two: two balls on screen,the level increases when you reach certain scores. the problem here when player loses the game restart at level 2 and not at level 0.here is my code:

public class MyGdxGame extends ApplicationAdapter implements InputProcessor{

// variables
public int[] x={0,0};
public int[] y={0,300};
public int[] size={100,50};
public Ball ball1,ball2;
public Player player;
// Checker: Checks if Ball Hits Player
public Checker checker;
public Texture background;
public Stage stage;

// creation of application
@Override
public void create () {
    checker=new Checker();
    player =new Player();
    background=new Texture(Gdx.files.internal("background.png"));
    stage = new Stage();
    // Ball(x, y, size , speedX, speedY)
    ball1=new Ball(x[0],y[0],size[0],10,10);
    ball2=new Ball(x[1],y[1],size[1],20,10);
}

// changement of application
@Override
public void render () {
    Gdx.gl.glClearColor(1,1,1,1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    stage.getBatch().begin();
    stage.getBatch().draw(background,0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
    stage.addActor(player);
    if(checker.level>0)stage.addActor(ball1);
    if(checker.level>1)stage.addActor(ball2);
    stage.getBatch().end();

    // Checks if Ball Hits Player
    if(checker.check1(player,ball1)){lost();}
    if(checker.check1(player,ball2)){lost();}

    // score increases
    checker.score();

    stage.act(Gdx.graphics.getDeltaTime());
    stage.draw();

    Gdx.input.setInputProcessor(this);
}
// Moethod Called When you lose
public void lost(){
    checker.level=0;
    checker.score=0;
}

// Drag Listener
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
    player.updatePosition(screenX,screenY);
    return true;
}

// releasing memory of the app
@Override
public void dispose () {
    stage.dispose();
}
\$\endgroup\$
1
\$\begingroup\$

You call stage.addActor(ball1) but never call ball1.remove() inside lost().

Same for ball2.

// Method called when you lose
public void lost(){
    checker.level=0;
    checker.score=0;
    ball1.remove();
    ball2.remove();
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.