⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gametask.java

📁 Developing Games in Java 源代码
💻 JAVA
字号:
package com.brackeen.javagamebook.scripting;

/**
    A game task that can be scheduled for one-time execution.
*/
public class GameTask implements Runnable {

    private long remainingTime;
    private Runnable runnable;
    private boolean done;

    /**
        Creates a new GameTask that will execute the specified
        Runnable after a delay.
    */
    public GameTask(long delay, Runnable runnable) {
        this.remainingTime = delay;
        this.runnable = runnable;
    }


    /**
        Cancels this task.
    */
    public void cancel() {
        done = true;
    }


    /**
        Runs this task.
    */
    public void run() {
        if (runnable != null) {
            runnable.run();
        }
    }


    /**
        Checks to see if this GameTask is ready to execute, and if
        so, it is executed. Returns true if the task is done
        (either it executed or previously canceled).
    */
    public boolean check(long elapsedTime) {
        if (!done) {
            remainingTime-=elapsedTime;
            if (remainingTime <= 0) {
                done = true;
                try {
                    run();
                }
                catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }

        return done;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -