OSDN Git Service

- setTimeout を setInterval に変更 (SWFヘッダを読んで FrameRate が分かった時にタイマーをかけ直し)
authorYoshihiro Yamazaki <yoya@awm.jp>
Tue, 4 Dec 2012 13:11:28 +0000 (22:11 +0900)
committerYoshihiro Yamazaki <yoya@awm.jp>
Tue, 4 Dec 2012 13:11:28 +0000 (22:11 +0900)
- timer に匿名関数を渡すように。

src/main.js

index 8ec9beb..a371f3a 100644 (file)
@@ -18,6 +18,7 @@ Flapp = function(url, canvas_id) {
     this.header = null
 //     this.frameTick = 1000 / 10; // default 0.1[sec]
     this.frameTick = 1000; // default
+    this.timerId = null;
 };
 Flapp.prototype = {
     play: function() {
@@ -34,21 +35,30 @@ Flapp.prototype = {
 //         this.frameTick = 1000 / header.framerate;
         this.frameTick = 1000;
         this.movieClip.totalframes = header.framecount;
+       if (typeof this.timerId === 'number') {
+           clearInterval(this.timerId);
+            this.ticks();
+       }
     },
     run: function(dict, movieClip, canvas) {
         console.debug("Flapp::run");
         var flapp = this;
         this.canvas = canvas;
-        this.tick(flapp, this.dict, this.movieClip);
+        this.ticks();
     },
-    tick: function(flapp, dict, movieClip) {
-        console.debug("Flapp::tick "+movieClip.currentFrame);
-        setTimeout(flapp.tick, flapp.frameTick, flapp, dict, movieClip);
-        if (movieClip.control(dict)) {
-            movieClip.action();
-            movieClip.render(flapp.canvas);
-            movieClip.increment();
-        }
+    ticks: function() {
+       var flapp = this;
+       var dict = this.dict;
+       var movieClip = this.movieClip;
+       var canvas = this.canvas;
+        console.debug("Flapp::ticks "+movieClip.currentFrame);
+        this.timerId = setInterval(function() {
+            if (movieClip.control(dict)) {
+               movieClip.action();
+               movieClip.render(canvas);
+               movieClip.increment();
+            }
+       }, flapp.frameTick);
     }
 };