Quantcast
Channel: Question and Answer » libgdx
Viewing all articles
Browse latest Browse all 434

Delaying certain actions/functions in libgdx using Timer?

$
0
0

Hello I am trying to delay a part of the processes in my game, but it happend that I use one of my variables in an inner class so I have to make it final, but by making it final I can’t assign new value when I find it. All I am asking for is to explain me how to do the delay without making my variable final.
Here is my code:

public class TomatoScript implements IActorScript {


private CompositeActor tomato;
private static final int speed = 100;
private BitmapFont bFont;
private Batch bch;
ArrayList<FruitScript> fruits;
private final int dmg = 10;
private int hp = 50;
private Rectangle bounds;
private int state = 0;
private FruitScript collidetFruit;

private boolean dead() {
    return hp <= 0;
}

private void setHp(int dmg) {
    hp = hp - dmg;
}


public TomatoScript(Batch b, BitmapFont bf, ArrayList<FruitScript> fruits) {
    this.bch = b;
    this.bFont = bf;
    this.fruits = fruits;
}

@Override
public void init(CompositeActor entity) {
    this.tomato = entity;
    tomato.setPosition(200, 129);
    bounds = new Rectangle(tomato.getX(), tomato.getY(), tomato.getWidth(), tomato.getHeight());
    bch.begin();
    bFont.draw(bch, Integer.toString(hp), tomato.getX() + 20, tomato.getY() + 95);
    bch.end();


}

@Override
public void act(float delta) {
    switch (state) {
        case 0:
            tomato.setX(tomato.getX() + speed * delta);
            bounds.setX(tomato.getX());
            bch.begin();
            bFont.draw(bch, Integer.toString(hp), tomato.getX() + 20, tomato.getY() + 95);
            bch.end();
            if (tomato.getX() >= 1090) {
                tomato.remove();
            }
            if (collisionFound()) {
                state++;
            }
            break;
        case 1:
            if (collisionAct(collidetFruit)) {
                state = 0;
            }
            break;


    }


}


private boolean collisionFound() {
    for (FruitScript f : fruits) {
        if (f.getBounds().overlaps(bounds)) {
            collidetFruit = f;
            return true;
        }

    }
    return false;

}

private boolean collisionAct(FruitScript f) {
    com.badlogic.gdx.utils.Timer.schedule(new com.badlogic.gdx.utils.Timer.Task() {
        @Override
        public void run() {
            f.setCol(true);//here it says I should make the "f" final.
            f.setHp(dmg);//here also.
            setHp(f.getDmg());
        }
    },2);

    if (f.dead()) {
        fruits.remove(f);
        return true;
    }
    if (dead()) {
        if (!f.dead()) {
            f.setCol(false);
            tomato.getScripts().remove(this);
            tomato.remove();
        }
        return false;
    }

    return false;
}

@Override
public void dispose() {
}}

I’ve tried to make it final, but it ruins everything it’s supposed to stop the actors do the damage taking and then the one alive to continue, but it was doing it too fast in a blink of an eye. When you make the f final, it can’t get the new item it should collide with. I hope someone can help :)


Viewing all articles
Browse latest Browse all 434

Trending Articles