浏览代码

🐛 Fix delta to not be Infinity on the first frame

Pedro Schneider 3 年之前
父节点
当前提交
181c642864
共有 1 个文件被更改,包括 8 次插入3 次删除
  1. 8 3
      pandora/handlers/GameHandler.js

+ 8 - 3
pandora/handlers/GameHandler.js

@@ -15,7 +15,7 @@ class GameHandler
         this.bDrawDebugFPS = val;
     }
 
-    static init()
+    static init(fps=60)
     {
         if (!this.renderMode) this.renderMode = RENDER_MODES.P2D;
         switch (this.renderMode)
@@ -28,6 +28,7 @@ class GameHandler
                 break;
 
         }
+        frameRate(fps);
         smooth();
     }
 
@@ -42,10 +43,13 @@ class GameHandler
         this.rootObjects.push(obj);
     }
 
+    static prevMillis = 0;
+    static delta = 0;
     static update()
     {
+        this.delta = (millis() - this.prevMillis) / 1000;
         for (let i = 0; i < this.rootObjects.length; i++)
-            this.rootObjects[i].update(1 / frameRate());
+            this.rootObjects[i].update(this.delta);
     }
 
     static draw()
@@ -60,6 +64,7 @@ class GameHandler
         }
 
         for (let i = 0; i < this.rootObjects.length; i++)
-            this.rootObjects[i].draw(1 / frameRate());
+            this.rootObjects[i].draw(this.delta);
+        this.prevMillis = millis();
     }
 }