在 libgdx 游戏中设置延迟

2022-01-12 00:00:00 delay java libgdx

我有一个游戏(比如超级跳投,这个游戏是一个跳跃游戏),我们的角色有生命.与敌人碰撞后,他的生命减少.我想在 1 秒后计算碰撞.我的意思是在这 1 秒内,如果我的角色与敌人接触,什么都不会发生,他会继续前进.为此,我在 GameScreen 类中定义了一个布尔变量,名称为collision",在 Wolrd 类中定义另一个,名称为collBirds".在与敌人碰撞一次接触后,collBirds 变为 true.但我想在 1 秒碰撞后更改为假.我使用了一些东西,比如 System.currentTimeMillis() 和for loop",但什么也没发生.我的java不太好.

i have a game(like super jumper, this game is a jumping game) that our character has life. after collision with enemies, his life reduce. and i want to after 1 sec , calculate the collisions. i mean in this 1 sec, if my character contact with enemies , nothing happen and he continue his way. for this , i define a boolean variable in my GameScreen class, name "collision" and another in Wolrd class, name "collBirds". after one contact with enemy collision and collBirds change to true. but i want after 1 sec collistion change to false. i use several things like System.currentTimeMillis() and "for loop",and nothing happen. i'm not so good in java.

这是我的条件:

if(World.collBirds == true && collition == false){
        life -= 1;
        lifeString = "Life : " + life;
        World.collBirds = false;
        collition = true;
        for (??? "need to stay here for 1 sec" ???) {
            collition = false;
        }
    }

推荐答案

在某些情况下,您可能还想使用 com.badlogic.gdx.utils.Timer

In some cases you could also want to use com.badlogic.gdx.utils.Timer

示例用法:

float delay = 1; // seconds

Timer.schedule(new Task(){
    @Override
    public void run() {
        // Do your work
    }
}, delay);

相关文章