在屏幕libgdx中添加分数

时间:2016-08-29 20:07:47

标签: java android libgdx

我有一个与障碍物碰撞的精灵,但我的屏幕顶部的分数有问题。 在我的类的更新中,碰撞由数组指定,但是当它与对象碰撞时,分数仍然在增长,我无法阻止它。这是我的代码:

private Array<Polen> polen;
private Score score;

    public PlayState(GameStateManager gsm) {
    super(gsm);
    score = new Score(110, 310);
    polen = new Array<Polen>();
    for(int i = 1; i <= COUNT; i++){
        polen.add(new Polen(i * (Polen.WIDTH)));
    }
}

    @Override
public void update(float dt) {
    handleInput();

    for(int i = 0; i < polen.size; i++){
        Polen pol= polen.get(i);

        if(pol.collides(aliado.getBounds())) {
            pol.changeExplosion();
            flagScore = 1;
        }
        if (pol.collides(aliado.getBounds())==false){
            flagScore = 0;
        }
    }
    if (flagScore == 1){
            Score.count++;
            flagScore=0;
            //auxCount = Score.count +1;

    }
    score.update(dt);
    updateGround();
    cam.update();

}

分数等级:

公共课成绩{

private static final int MOVEMENT = 70;
private Vector3 position;
private Vector3 velocity;
private Rectangle bounds;
private Texture texture;

public Score(int x, int y){
    position = new Vector3(x, y, 0);
    velocity = new Vector3(0, 0, 0);
    texture = new Texture("score0.png");
    bounds = new Rectangle(x, y, ((texture.getWidth())), texture.getHeight());
}



public void update(float dt){//code for move the score for top of screen:
    if(position.y > 0)
        velocity.add(0, 0, 0);
    velocity.scl(dt);
    position.add(MOVEMENT * dt, velocity.y, 0);
    if(position.y < 0)
        position.y = 0;

    velocity.scl(1/dt);
    bounds.setPosition(position.x, position.y);

}

public Vector3 getPosition() {
    return position;
}

public Texture getTexture() {
    return texture;
}

public void setTexture(Texture texture) {
    this.texture = texture;
}

public Vector3 getVelocity() {
    return velocity;
}

public void setVelocity(Vector3 velocity) {
    this.velocity = velocity;
}

public Rectangle getBounds(){
    return bounds;
}

public void setBounds(Rectangle bounds) {
    this.bounds = bounds;
}

public void dispose(){
    texture.dispose();
}

}

1 个答案:

答案 0 :(得分:1)

pol.changeExplosion()中,你应该添加一个布尔值来将对象标记为已完成。

public class Polen {
  private boolean exploded = false;

  public void changeExplosion() {
    // ...
    exploded = true;
    // ...
  }

  public boolean isExploded() {
    return exploded;
  }
}

然后,在您的更新功能中,您可以使用此标志来确定分数是否应该停止递增。

for(int i = 0; i < polen.size; i++) {
  Polen pol= polen.get(i);

  if(pol.collides(aliado.getBounds()) && !pol.isExploded()) {
    pol.changeExplosion();
    flagScore = 1;
  }
  else if (!pol.collides(aliado.getBounds())) {
    flagScore = 0;
  }
}