OSDN Git Service

- setTimeout を setInterval に変更 (SWFヘッダを読んで FrameRate が分かった時にタイマーをかけ直し)
[flapp/flapp.git] / src / main.js
1 goog.provide('Flapp');
2 goog.require('FlappLoader');
3 goog.require('FlappDict');
4 goog.require('FlappCanvas');
5 goog.require('FlappMovieClip');
6
7 goog.scope(function() {
8
9 /**
10  * @constructor
11  */
12 Flapp = function(url, canvas_id) {
13     console.debug("Flapp("+url+","+canvas_id+")");
14     this.url = url;
15     this.canvas_id = canvas_id;
16     this.canvas = document.getElementById(canvas_id);
17     goog.global.flapp = this; // debug
18     this.header = null
19 //      this.frameTick = 1000 / 10; // default 0.1[sec]
20     this.frameTick = 1000; // default
21     this.timerId = null;
22 };
23 Flapp.prototype = {
24     play: function() {
25         console.debug("Flapp.prototype.play");
26         var flapp = this;
27         var loader = new FlappLoader(flapp); // file loader
28         this.dict = new FlappDict(); // content dictionary
29         this.movieClip = new FlappMovieClip("_root", null, null, null); // root MC
30         loader.fromURL(this.url, this.dict, this.movieClip);
31         this.canvas = new FlappCanvas(this.canvas);
32         this.run(this.dict, this.movieClip, this.canvas);
33     },
34     setHeader: function(header) {
35 //          this.frameTick = 1000 / header.framerate;
36         this.frameTick = 1000;
37         this.movieClip.totalframes = header.framecount;
38         if (typeof this.timerId === 'number') {
39             clearInterval(this.timerId);
40             this.ticks();
41         }
42     },
43     run: function(dict, movieClip, canvas) {
44         console.debug("Flapp::run");
45         var flapp = this;
46         this.canvas = canvas;
47         this.ticks();
48     },
49     ticks: function() {
50         var flapp = this;
51         var dict = this.dict;
52         var movieClip = this.movieClip;
53         var canvas = this.canvas;
54         console.debug("Flapp::ticks "+movieClip.currentFrame);
55         this.timerId = setInterval(function() {
56             if (movieClip.control(dict)) {
57                 movieClip.action();
58                 movieClip.render(canvas);
59                 movieClip.increment();
60             }
61         }, flapp.frameTick);
62     }
63 };
64
65 goog.exportSymbol('Flapp', Flapp);
66 goog.exportSymbol('Flapp.prototype.play', Flapp.prototype.play);
67
68 });