libgdx计时器倒计时实施

时间:2017-04-25 09:12:10

标签: java timer libgdx

我想在LibGDX框架中创建高效的计时器,它将计算我的角色剩下的时间。应将doSmth()方法调用多次,因为某些标志设置为true。我知道Timer的第三个参数是它应该触发多少次。现在一个Im调用该方法递归,但我不认为这是有效的方法。

 public void updateTimer(){
        new Timer().scheduleTask(new Timer.Task() {
    @Override
    public void run() {
      doSmth();
     updateTimer();
    }
},1);
            }

2 个答案:

答案 0 :(得分:2)

使用重复计数会更准确。每次运行任务时,您的方法都会引入一些错误,因为任务是在GL线程上运行的,因此它会在一秒钟之后稍微发生,但在此之后您会重复一秒钟。因此,每次重复都会稍稍落后。

private Timer.Task myTimerTask = new Timer.Task() {
    @Override
    public void run() {
      doSmth();
    }
};

public void startTimer(){
    Timer.schedule(myTimerTask, 1f, 1f);
}

当你需要阻止它时:

myTimerTask.cancel();

答案 1 :(得分:1)

com.badlogic.gdx.utils.Timer将来会在主循环线程上执行任务,即使您的游戏处于暂停屏幕,菜单或其他状态,您也可以简单地控制渲染方法中的时间通过添加增量时间。

    private float timeSeconds = 0f;
    private float period = 1f;

    public void render() {
        //Execute handleEvent each 1 second
        timeSeconds +=Gdx.graphics.getRawDeltaTime();
        if(timeSeconds > period){
            timeSeconds-=period;
            handleEvent();
        }

        [...]

    }

    public void handleEvent() {
         [...]
    }

为了保持井井有条,我个人在我的主游戏类上有一个数组,它包含我所有的定时事件并处理渲染周期中的所有内容。在您的情况下,您可以根据需要设置一些控制变量。

我的实施示例:

// MainGame.java

 private ObjectMap<TimedEventEnum, TimedEvent> hshTimedEvent;

 public void render(){
    executeTimedEvents();
 }

 private void executeTimedEvents() {
    for (ObjectMap.Entry<TimedEventEnum, TimedEvent> entry : hshTimedEvent) {
            TimedEvent event = entry.value;
            event.process();
    }
 }

 public void killEvent(TimedEventEnum event) {
        hshTimedEvent.remove(event);
 }

// TimedEventEnum.java

public enum TimedEventEnum {
        COUNT_MONEY,
        CHECK_FOR_ACHIEVS,
        ANOTHER_EVENT_EXAMPLE
    }

// CountMoneyTimedEvent.java

public class CountMoneyTimedEvent extends Timer implements TimedEvent {

        public CountMoneyTimedEvent() {
            super();
            init(this, 4f, false);
        }

        @Override
        public void execute() {
            //execute logic here
        }

        @Override
        public void reset() {
            this.timesFired = 0L;
        }
    }

// Timer.java

public abstract class Timer {

        private Float deltaCount;
        private Float timeToEvent;
        private Boolean isRepeatable;
        protected Long timesFired;

        private TimedEvent event;

        Timer() {
        }

        public void init(TimedEvent event, Float eventTime, Boolean isRepeatable) {
            this.deltaCount = 0f;
            this.timeToEvent = eventTime;
            this.isRepeatable = isRepeatable;
            this.timesFired = 0L;
            this.event = event;
        }

        public void process() {
            if (isEventTime()) {
                event.execute();
            }
        }

        private Boolean isEventTime() {
            if (event != null && (isRepeatable || timesFired == 0)) {
                deltaCount += Gdx.graphics.getRawDeltaTime();
                if (deltaCount > timeToEvent) {
                    deltaCount -= timeToEvent;
                    timesFired++;
                    return true;
                }
            }
            return false;
        }

        protected void executeNextEvent() {
            deltaCount = timeToEvent;
        }
    }

// TimedEvent.java

public interface TimedEvent {
    void execute();
    void reset();
    void process();
}