Phaser.MainLoop = function (game, framerate, forceSetTimeOut){ if (framerate === undefined) { framerate = 60; } if (forceSetTimeOut === undefined) { forceSetTimeOut = false ; } this.game = game; this.timestep = 1000 / framerate; this.physicsStep = 1 / framerate; this.frameDelta = 0; this.lastFrameTimeMs = 0; this.framerate = framerate; this.fps = framerate; this.lastFpsUpdate = 0; this.framesThisSecond = 0; this.numUpdateSteps = 0; this.minFrameDelay = 0; this.running = false ; this.started = false ; this.panic = false ; this._isSetTimeOut = false ; this._handleID = null ; } ; Phaser.MainLoop.prototype = { start: function (){ if (this.started) { return this; } this.started = true ; this.running = true ; this.lastFrameTimeMs = window.performance.now(); this.lastFpsUpdate = window.performance.now(); this.framesThisSecond = 0; if (!window.requestAnimationFrame || this.forceSetTimeOut) { this._isSetTimeOut = true ; this._handleID = _AN_Call_settimeout('setTimeout', window, this.step.bind(this), 0); } else { this._isSetTimeOut = false ; this._handleID = window.requestAnimationFrame(this.step.bind(this)); } } , step: function (timestamp){ this.frameDelta += timestamp - this.lastFrameTimeMs; this.lastFrameTimeMs = timestamp; this.begin(timestamp); if (timestamp > this.lastFpsUpdate + 1000) { this.fps = 0.25 * this.framesThisSecond + 0.75 * this.fps; this.lastFpsUpdate = timestamp; this.framesThisSecond = 0; } this.framesThisSecond++ ; this.numUpdateSteps = 0; while (this.frameDelta >= this.timestep){ this.update(this.timestep); this.frameDelta -= this.timestep; if (++this.numUpdateSteps >= 240) { this.panic = true ; break ; } } this.render(this.frameDelta / this.timestep); this.panic = false ; this._handleID = window.requestAnimationFrame(this.step.bind(this)); } , begin: function (timestamp){ this.game.time.update(timestamp); this.game.scale.preUpdate(timestamp, this.frameDelta); this.game.debug.preUpdate(timestamp, this.frameDelta); this.game.camera.preUpdate(timestamp, this.frameDelta); this.game.physics.preUpdate(timestamp, this.frameDelta); this.game.state.preUpdate(timestamp, this.frameDelta); this.game.plugins.preUpdate(timestamp, this.frameDelta); this.game.stage.preUpdate(timestamp, this.frameDelta); } , update: function (timestep){ this.game.state.update(timestep); this.game.stage.update(timestep); this.game.tweens.update(timestep); this.game.sound.update(timestep); this.game.input.update(timestep); this.game.physics.update(timestep); this.game.particles.update(timestep); this.game.plugins.update(timestep); this.game.stage.postUpdate(timestep); this.game.plugins.postUpdate(timestep); this.game.stage.updateTransform(); } , render: function (dt){ this.game.renderer.renderSession.interpolation = dt; this.game.state.preRender(dt); if (this.game.renderType !== Phaser.HEADLESS) { this.game.renderer.render(this.game.stage); this.game.plugins.render(dt); this.game.state.render(dt); } this.game.plugins.postRender(dt); } , stop: function (){ this.running = false ; this.started = false ; if (this._isSetTimeOut) { clearTimeout(this._handleID); } else { window.cancelAnimationFrame(this._handleID); } return this; } , resetFrameDelta: function (){ var oldFrameDelta = this.frameDelta; this.frameDelta = 0; return oldFrameDelta; } } ; Object.defineProperty(Phaser.MainLoop.prototype, 'maxFPS', { get: function (){ return 1000 / this.minFrameDelay; } , set: function (value){ if (fps === 0) { this.stop(); } else { this.minFrameDelay = 1000 / value; } } } ); Phaser.MainLoop.prototype.constructor = Phaser.MainLoop; Phaser.NOOP = function (){ } ;