私が正しくあなたの質問を理解していれば、あなたはあなたのコードは、(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);
}
}
フロートとダブルを等価で比較することはほとんどありません。正確な数値ではありません。あなたが私たちに示したコードのほんの少しが私には大丈夫だと思いますが、「不正確な結果」を意味するものを記述できますか? –
私は自由落下時間を入力できるフリーフォール・シミュレータを作ろうとしています。タイマーが不正確で、時々計算がオフになることがあります。 –
@Todd Sewell私はちょうど停止するif文をwan'tしません2秒 –