【问题标题】:Best way to pause and resume a SpriteBatch animation that occurs every n seconds暂停和恢复每 n 秒发生的 SpriteBatch 动画的最佳方法
【发布时间】:2014-05-08 14:52:46
【问题描述】:

您好,我正在尝试在我的 libgdx 游戏中实现暂停功能,但在暂停每 5 秒发生一次的动画时遇到问题。在我的渲染方法中,我每 5 秒重新启动一次动画。动画完成后,我停止绘制它,它们按预期工作,每 5 秒重新启动和绘制一次。然而,当我按下暂停按钮时,当前动画结束,这没关系,但一旦我取消暂停游戏,它会立即重新绘制另一个动画,而不是等到下一个 5 秒时间段。我认为这是因为一旦我取消暂停,TimeUtils.millis() 仍然大于 5 秒,导致它立即再次触发,所以我将如何重置计时以防止这种情况发生。谢谢。

 if(TimeUtils.millis()>=(TimePassed+timekeep.timecheck)&& paused==false)
                    {            //every N seconds and if not paused  
     System.out.println("Rendering.....");

     stateTime =0;                               //reset animation
        for(int i=0;i<=timekeep.rndy-1;i++){
            c.e.get(i).alive=true;  //set all characters to alive to be drawn
        }
   TimePassed = TimeUtils.millis();   //this is the time passed since last restart

    }

【问题讨论】:

    标签: java android animation libgdx


    【解决方案1】:

    您可以测量游戏暂停的时间,并在您取消暂停游戏时将其添加到 TimePassed。那么 TimePassed + 5 sec 应该比 TimeUtils.millis() 小。

    【讨论】:

    • 谢谢我把它整理好了。
    【解决方案2】:

    在某处声明您的 _time 浮点值。

    float _time=0f;
    

    并在更新或渲染方法中添加增量时间(自上次更新以来经过的时间量)

    _time+=Gdx.graphics.getDeltaTime();
    

    现在检查 _time 是否大于 5

     if(_time>5)
    {
      // do your stuff & reset the timer
     _time=0;
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-21
      • 1970-01-01
      • 1970-01-01
      • 2012-04-17
      • 2015-07-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多