1
function airEngineJS (canvasArg, properties) {

    this.canvas = (canvasArg == "undefined")? trace("Failed to set up canvas for airEngineJS") : canvasArg;
    this.cameras = [];
    this.displayObjects = [];
    this.hudDisplayObjects = [];
    this.fps = (properties == "undefined" || properties.fps == "undefined")? 30 : properties.fps;

    if (properties == "undefined" || properties.autoinit == "undefined" || properties.autoinit == true){
      this.keyboard = new keyboardJS();
      this.keyboard.init();
      this.cameras.push(new airCameraJS(this));
    }
    this.enterframe = setInterval(this.intervalCaller, 1000/this.fps);
    trace("A new airEngineJS has been created");
  }
  airEngineJS.prototype = {
    intervalCaller : function () {
      this.mainLoop();
    },
    logic : function () {
      for (var i = 0; i < this.displayObjects.length; ++i){
        this.displayObjects[i].logic();
      }
      for (var j = 0; j < this.cameras.length; ++j){
        this.cameras[j].logic();
      }
    },
    render : function () {
      for (var i = 0; i < this.cameras.length; ++i){
        for (var j = 0; j < this.displayObjects.length; ++j){
          this.displayObjects[j].renderOn(this.cameras[i]);
        }
      }
      for (var i = 0; i < this.hudDisplayObjects.length; ++i){
        this.hudDisplayObjects[i].renderOn(this.canvas);
      }
    },
    mainLoop : function () {
      this.logic();
      this.render();
    }
  }

間隔[this.enterframe=setInterval(this.intervalCaller、1000 / this.fps);]は正しく(this.intervalCaller)を呼び出しますが、これはDOMで(this.mainLoop())を呼び出そうとします。

どのようにすればよいかについての提案はありますか?:(

4

1 に答える 1

1

次のように、実際の関数呼び出しを閉じることができます。

var self = this;
this.enterframe = setInterval(function() {
    self.intervalCaller();
}, 1000/this.fps);

最初に新しいインスタンスへの参照を作成し、それを に渡されたクロージャ内で使用しsetIntervalます。

于 2012-05-23T15:52:33.137 に答える