2016-11-20 7 views
0

基本的に私がタイマーを作成するのはfloat timer += 1 * deltaTimeで、1秒ごとに1回追加されますが、使用に問題があります。Libgdxで正確なタイマーを作成するには?

if(timer < 2){ 
    //do something 
} 

私はそれがあまりにも速いので、それも2秒は検出されませんので、タイマーが2秒であるとき、私はif(timer != 2f)を行うことができないので、購入のコードの実行を停止するためにif文をしたいです。これは、私が条件timer < 2fを入れなければならないことを意味します。これは正確ではなく、常に私には不十分な結果を与えます。

+0

フロートとダブルを等価で比較することはほとんどありません。正確な数値ではありません。あなたが私たちに示したコードのほんの少しが私には大丈夫だと思いますが、「不正確な結果」を意味するものを記述できますか? –

+0

私は自由落下時間を入力できるフリーフォール・シミュレータを作ろうとしています。タイマーが不正確で、時々計算がオフになることがあります。 –

+0

@Todd Sewell私はちょうど停止するif文をwan'tしません2秒 –

答えて

0

手作りタイマーを使用する代わりに、なぜTaskとブール値を使用しないのですか?

下記の擬似コード。

boolean stop = false; 

Timer.schedule(new Task() { 

    @Override 
    public void run() { 
     stop = true; 
    } 
}, 2f); 

次に、レンダリングメソッドで、

render() { 

    if (!stop) { 

     // Freefall 
    } 
} 
0

私が正しくあなたの質問を理解していれば、あなたはあなたのコードは、(Xは、12.556秒などの浮動小数点値にすることができます)X秒とこれ以上のために処理したいです。

以下に示すように、フリーフォールロジックを管理するカスタムTimerクラスがあるシングルスレッドの代替案を提案します。進行状況を監視し、継続時間よりも何かが見える場合は、内部ロジックに「嘘」をつけて、指定された時間だけロジックが実行されるようにします(浮動小数点数を使用しているので、 。

以下の例は、libGDXを使用しないでこの種のことを示していますが、他のライブラリを使用している人には興味がありますが、私のシミュレートしたgetDeltaTime()メソッドをGdx.graphics.getDeltaTime()に置き換えるのは簡単です。

package tech.otter.timing; 

/** 
* Created by john on 11/20/16. 
*/ 
public class TimingExample { 
    public static void main(String... args) { 
     boolean complete = false; 

     Timer t = new Timer(15f) { 
      // You could implement your logic here. 
      @Override 
      void action(float delta) { 
       System.out.println(progress); 
      } 
     }; 

     while(!complete) { 
      complete = t.update(getDeltaTime()); 
     } 

     assert t.progress < t.duration; 
     assert t.progress + t.errorMargin > t.duration; 
    } 

    /** 
    * Simulates processing time by returning 0-100ms. 
    * @return The number of milliseconds that have allegedly elapsed since the last call. 
    */ 
    public static float getDeltaTime() { 
     return (float)(Math.random()/10); 
    } 

    abstract static class Timer { 
     private float duration; 
     protected float progress; 
     private float errorMargin; 

     public Timer(float duration) { 
      this(duration, 0.0001f); 
     } 
     public Timer(float duration, float errorMargin) { 
      this.duration = duration; 
      this.errorMargin = errorMargin; 
      this.progress = 0f; 
     } 

     /** 
     * Update the timer based on how long since the last call. 
     * @param delta The amount of time since the last call. 
     * @return Whether the timer's progressed has met the duration. 
     */ 
     public boolean update(float delta) { 
      // This if-statement "caps" the delta so that we will never exceed the duration. 
      if(progress + delta > duration) { 
       delta = duration - progress; 
      } 
      progress += delta; 
      action(delta); 

      // Return "true" if the progress is equal to the duration (+/- a small margin just in case since we use floats). 
      return progress + errorMargin > duration && progress - errorMargin < duration; 
     } 

     /** 
     * Override this method with your game logic. 
     * You should not call it directly. 
     * @param delta The amount of time that has elapsed since the timer was last updated. 
     */ 
     abstract void action(float delta); 
    } 
} 
関連する問題