add score in Screen libgdx

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



add score in Screen libgdx



I have a sprite that collides with obstacle, but i have a problem with the score in the top of my screen.
In the update of my class, The collisions are specified by an array, but when it collides with an object, the score is still growing and I can not stop it. this is my code:


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();




Score Class:



public class Score


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 Answer
1



In pol.changeExplosion() you should add a boolean to mark the object as done.


pol.changeExplosion()


public class Polen
private boolean exploded = false;

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


public boolean isExploded()
return exploded;




Then, in your update function you can use this flag to determine if the score should stop being incremented.


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;







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS