简体   繁体   English

如何使布尔值持续一定时间?

[英]How do I make boolean last for a certain amount of time?

I am trying to make an effect for my game that when the player uses it, the character runs faster, but I want to make the sodaEffect last for a certain amount of time, but i'm not sure how to do that. 我正在尝试为游戏创造一种效果,当玩家使用它时,角色运行得更快,但是我想使sodaEffect持续一定的时间,但是我不确定该怎么做。 I am using Slick2D and LWJGL to make the game. 我正在使用Slick2D和LWJGL制作游戏。

    public class MainMap extends BasicGameState {

    if(input.isKeyPressed(Input.KEY_I)) {
        sbg.enterState(5);
    }
    if(InventoryClass.sodaEffect == true) {
        InventoryClass.characterSpeed = 1f;
    }
    else{
        InventoryClass.characterSpeed = .1f;
    }}

    public class InventoryClass {

    public static boolean sodaEffect = false;
    }

Without exactely knowing what your InventoryClass does, I would do something like this: 在不完全知道您的InventoryClass做什么的情况下,我会做这样的事情:

public class InventoryClass {
    private final static long SODA_DURATION = 5000L;
    private static long sodaStartTime;

    public static void startSodaEffect() {
        sodaStartTime = System.currentTimeMillis();
    }

    public static boolean hasSodaEffect() {
        return (System.currentTimeMillis() - sodaStartTime) < SODA_DURATION;
    }
}

With this approach, you don't need additional libraries. 使用这种方法,您不需要其他库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM