diff --git a/Phaser/Game.ts b/Phaser/Game.ts index 36698a7c..86e3e660 100644 --- a/Phaser/Game.ts +++ b/Phaser/Game.ts @@ -60,6 +60,8 @@ module Phaser { */ constructor(callbackContext, parent?: string = '', width?: number = 800, height?: number = 600, initCallback = null, createCallback = null, updateCallback = null, renderCallback = null, destroyCallback = null) { + this.id = Phaser.GAMES.push(this) - 1; + this.callbackContext = callbackContext; this.onInitCallback = initCallback; this.onCreateCallback = createCallback; @@ -69,16 +71,21 @@ module Phaser { if (document.readyState === 'complete' || document.readyState === 'interactive') { - setTimeout(() => this.boot(parent, width, height)); + //setTimeout(() => this.boot(parent, width, height)); + setTimeout(() => Phaser.GAMES[this.id].boot(parent, width, height)); } else { - document.addEventListener('DOMContentLoaded', () => this.boot(parent, width, height), false); - window.addEventListener('load', () => this.boot(parent, width, height), false); + //document.addEventListener('DOMContentLoaded', () => this.boot(parent, width, height), false); + //window.addEventListener('load', () => this.boot(parent, width, height), false); + document.addEventListener('DOMContentLoaded', Phaser.GAMES[this.id].boot(parent, width, height), false); + window.addEventListener('load', Phaser.GAMES[this.id].boot(parent, width, height), false); } } + public id: number; + /** * Game loop trigger wrapper. */ @@ -308,10 +315,14 @@ module Phaser { if (!document.body) { - window.setTimeout(() => this.boot(parent, width, height), 13); + //window.setTimeout(() => this.boot(parent, width, height), 13); + setTimeout(() => Phaser.GAMES[this.id].boot(parent, width, height), 13); } else { + document.removeEventListener('DOMContentLoaded', Phaser.GAMES[this.id].boot); + window.removeEventListener('load', Phaser.GAMES[this.id].boot); + this.device = new Device(); this.net = new Net(this); this.math = new GameMath(this); diff --git a/Phaser/Phaser.ts b/Phaser/Phaser.ts index 5f1b6f47..ca955a47 100644 --- a/Phaser/Phaser.ts +++ b/Phaser/Phaser.ts @@ -19,4 +19,6 @@ module Phaser { export var VERSION: string = 'Phaser version 1.0.0'; + export var GAMES = []; + } diff --git a/Phaser/input/Input.ts b/Phaser/input/Input.ts index 0bb5164f..dea65d38 100644 --- a/Phaser/input/Input.ts +++ b/Phaser/input/Input.ts @@ -22,20 +22,20 @@ module Phaser { constructor(game: Game) { - this._game = game; + this.game = game; - this.mousePointer = new Pointer(this._game, 0); - this.pointer1 = new Pointer(this._game, 1); - this.pointer2 = new Pointer(this._game, 2); - this.pointer3 = new Pointer(this._game, 3); - this.pointer4 = new Pointer(this._game, 4); - this.pointer5 = new Pointer(this._game, 5); + this.mousePointer = new Pointer(this.game, 0); + this.pointer1 = new Pointer(this.game, 1); + this.pointer2 = new Pointer(this.game, 2); + this.pointer3 = new Pointer(this.game, 3); + this.pointer4 = new Pointer(this.game, 4); + this.pointer5 = new Pointer(this.game, 5); - this.mouse = new Mouse(this._game); - this.keyboard = new Keyboard(this._game); - this.touch = new Touch(this._game); - this.mspointer = new MSPointer(this._game); - this.gestures = new Gestures(this._game); + this.mouse = new Mouse(this.game); + this.keyboard = new Keyboard(this.game); + this.touch = new Touch(this.game); + this.mspointer = new MSPointer(this.game); + this.gestures = new Gestures(this.game); this.onDown = new Phaser.Signal(); this.onUp = new Phaser.Signal(); @@ -48,7 +48,7 @@ module Phaser { this._oldPosition = new Vec2; this.circle = new Circle(0, 0, 44); - this.camera = this._game.camera; + this.camera = this.game.camera; this.activePointer = this.mousePointer; this.currentPointers = 0; @@ -61,9 +61,9 @@ module Phaser { } /** - * Local private reference to game. + * Local reference to game. */ - private _game: Game; + public game: Game; /** * A 1x1 sized canvas used for pixel-perfect checks @@ -451,7 +451,7 @@ module Phaser { } else { - this['pointer' + next] = new Pointer(this._game, next); + this['pointer' + next] = new Pointer(this.game, next); return this['pointer' + next]; } @@ -563,7 +563,7 @@ module Phaser { this.currentPointers = 0; - this._game.stage.canvas.style.cursor = "default"; + this.game.stage.canvas.style.cursor = "default"; if (hard == true) { @@ -939,14 +939,14 @@ module Phaser { /** * @param {Camera} [camera] */ - public getWorldX(camera?: Camera = this._game.camera) { + public getWorldX(camera?: Camera = this.game.camera) { return camera.worldView.x + this.x; } /** * @param {Camera} [camera] */ - public getWorldY(camera?: Camera = this._game.camera) { + public getWorldY(camera?: Camera = this.game.camera) { return camera.worldView.y + this.y; } @@ -957,12 +957,12 @@ module Phaser { */ public renderDebugInfo(x: number, y: number, color?: string = 'rgb(255,255,255)') { - this._game.stage.context.fillStyle = color; - this._game.stage.context.fillText('Input', x, y); - this._game.stage.context.fillText('X: ' + this.x + ' Y: ' + this.y, x, y + 14); - this._game.stage.context.fillText('World X: ' + this.getWorldX() + ' World Y: ' + this.getWorldY(), x, y + 28); - this._game.stage.context.fillText('Scale X: ' + this.scale.x.toFixed(1) + ' Scale Y: ' + this.scale.x.toFixed(1), x, y + 42); - this._game.stage.context.fillText('Screen X: ' + this.activePointer.screenX + ' Screen Y: ' + this.activePointer.screenY, x, y + 56); + this.game.stage.context.fillStyle = color; + this.game.stage.context.fillText('Input', x, y); + this.game.stage.context.fillText('X: ' + this.x + ' Y: ' + this.y, x, y + 14); + this.game.stage.context.fillText('World X: ' + this.getWorldX() + ' World Y: ' + this.getWorldY(), x, y + 28); + this.game.stage.context.fillText('Scale X: ' + this.scale.x.toFixed(1) + ' Scale Y: ' + this.scale.x.toFixed(1), x, y + 42); + this.game.stage.context.fillText('Screen X: ' + this.activePointer.screenX + ' Screen Y: ' + this.activePointer.screenY, x, y + 56); } diff --git a/Phaser/input/Keyboard.ts b/Phaser/input/Keyboard.ts index 8709925d..1ab6fd2b 100644 --- a/Phaser/input/Keyboard.ts +++ b/Phaser/input/Keyboard.ts @@ -14,11 +14,17 @@ module Phaser { constructor(game: Game) { - this._game = game; + this.game = game; } - private _game: Game; + /** + * Local reference to game. + * @property game + * @type {Phaser.Game} + **/ + public game: Game; + private _keys = {}; private _capture = {}; @@ -28,10 +34,26 @@ module Phaser { */ public disabled: bool = false; + /** + * A reference to the event handlers to allow removeEventListener support + */ + public _onKeyDown; + public _onKeyUp; + public start() { - document.body.addEventListener('keydown', (event: KeyboardEvent) => this.onKeyDown(event), false); - document.body.addEventListener('keyup', (event: KeyboardEvent) => this.onKeyUp(event), false); + this._onKeyDown = (event: KeyboardEvent) => this.onKeyDown(event); + this._onKeyUp = (event: KeyboardEvent) => this.onKeyUp(event); + + document.body.addEventListener('keydown', this._onKeyDown , false); + document.body.addEventListener('keyup', this._onKeyUp, false); + + } + + public stop() { + + document.body.removeEventListener('keydown', this._onKeyDown); + document.body.removeEventListener('keyup', this._onKeyUp); } @@ -78,7 +100,7 @@ module Phaser { */ public onKeyDown(event: KeyboardEvent) { - if (this._game.input.disabled || this.disabled) + if (this.game.input.disabled || this.disabled) { return; } @@ -90,12 +112,12 @@ module Phaser { if (!this._keys[event.keyCode]) { - this._keys[event.keyCode] = { isDown: true, timeDown: this._game.time.now, timeUp: 0 }; + this._keys[event.keyCode] = { isDown: true, timeDown: this.game.time.now, timeUp: 0 }; } else { this._keys[event.keyCode].isDown = true; - this._keys[event.keyCode].timeDown = this._game.time.now; + this._keys[event.keyCode].timeDown = this.game.time.now; } } @@ -105,7 +127,7 @@ module Phaser { */ public onKeyUp(event: KeyboardEvent) { - if (this._game.input.disabled || this.disabled) + if (this.game.input.disabled || this.disabled) { return; } @@ -117,12 +139,12 @@ module Phaser { if (!this._keys[event.keyCode]) { - this._keys[event.keyCode] = { isDown: false, timeDown: 0, timeUp: this._game.time.now }; + this._keys[event.keyCode] = { isDown: false, timeDown: 0, timeUp: this.game.time.now }; } else { this._keys[event.keyCode].isDown = false; - this._keys[event.keyCode].timeUp = this._game.time.now; + this._keys[event.keyCode].timeUp = this.game.time.now; } } @@ -143,7 +165,7 @@ module Phaser { */ public justPressed(keycode: number, duration?: number = 250): bool { - if (this._keys[keycode] && this._keys[keycode].isDown === true && (this._game.time.now - this._keys[keycode].timeDown < duration)) + if (this._keys[keycode] && this._keys[keycode].isDown === true && (this.game.time.now - this._keys[keycode].timeDown < duration)) { return true; } @@ -161,7 +183,7 @@ module Phaser { */ public justReleased(keycode: number, duration?: number = 250): bool { - if (this._keys[keycode] && this._keys[keycode].isDown === false && (this._game.time.now - this._keys[keycode].timeUp < duration)) + if (this._keys[keycode] && this._keys[keycode].isDown === false && (this.game.time.now - this._keys[keycode].timeUp < duration)) { return true; } diff --git a/Phaser/input/MSPointer.ts b/Phaser/input/MSPointer.ts index 17939aa2..8207d072 100644 --- a/Phaser/input/MSPointer.ts +++ b/Phaser/input/MSPointer.ts @@ -20,17 +20,16 @@ module Phaser { */ constructor(game: Game) { - this._game = game; + this.game = game; } /** - * Local private reference to game. - * @property _game + * Local reference to game. + * @property game * @type Game - * @private **/ - private _game: Game; + public game: Game; /** * You can disable all Input by setting disabled = true. While set all new input related events will be ignored. @@ -38,17 +37,28 @@ module Phaser { */ public disabled: bool = false; + /** + * A reference to the event handlers to allow removeEventListener support + */ + public _onMSPointerDown; + public _onMSPointerMove; + public _onMSPointerUp; + /** * Starts the event listeners running * @method start */ public start() { - if (this._game.device.mspointer == true) + if (this.game.device.mspointer == true) { - this._game.stage.canvas.addEventListener('MSPointerDown', (event) => this.onPointerDown(event), false); - this._game.stage.canvas.addEventListener('MSPointerMove', (event) => this.onPointerMove(event), false); - this._game.stage.canvas.addEventListener('MSPointerUp', (event) => this.onPointerUp(event), false); + this._onMSPointerDown = (event) => this.onPointerDown(event); + this._onMSPointerMove = (event) => this.onPointerMove(event); + this._onMSPointerUp = (event) => this.onPointerUp(event); + + this.game.stage.canvas.addEventListener('MSPointerDown', this._onMSPointerDown, false); + this.game.stage.canvas.addEventListener('MSPointerMove', this._onMSPointerMove, false); + this.game.stage.canvas.addEventListener('MSPointerUp', this._onMSPointerUp, false); } } @@ -60,7 +70,7 @@ module Phaser { **/ private onPointerDown(event) { - if (this._game.input.disabled || this.disabled) + if (this.game.input.disabled || this.disabled) { return; } @@ -68,7 +78,7 @@ module Phaser { event.preventDefault(); event.identifier = event.pointerId; - this._game.input.startPointer(event); + this.game.input.startPointer(event); } @@ -79,7 +89,7 @@ module Phaser { **/ private onPointerMove(event) { - if (this._game.input.disabled || this.disabled) + if (this.game.input.disabled || this.disabled) { return; } @@ -87,7 +97,7 @@ module Phaser { event.preventDefault(); event.identifier = event.pointerId; - this._game.input.updatePointer(event); + this.game.input.updatePointer(event); } @@ -98,7 +108,7 @@ module Phaser { **/ private onPointerUp(event) { - if (this._game.input.disabled || this.disabled) + if (this.game.input.disabled || this.disabled) { return; } @@ -106,7 +116,7 @@ module Phaser { event.preventDefault(); event.identifier = event.pointerId; - this._game.input.stopPointer(event); + this.game.input.stopPointer(event); } @@ -116,11 +126,11 @@ module Phaser { */ public stop() { - if (this._game.device.mspointer == true) + if (this.game.device.mspointer == true) { - //this._game.stage.canvas.addEventListener('MSPointerDown', (event) => this.onPointerDown(event), false); - //this._game.stage.canvas.addEventListener('MSPointerMove', (event) => this.onPointerMove(event), false); - //this._game.stage.canvas.addEventListener('MSPointerUp', (event) => this.onPointerUp(event), false); + this.game.stage.canvas.removeEventListener('MSPointerDown', this._onMSPointerDown); + this.game.stage.canvas.removeEventListener('MSPointerMove', this._onMSPointerMove); + this.game.stage.canvas.removeEventListener('MSPointerUp', this._onMSPointerUp); } } diff --git a/Phaser/input/Mouse.ts b/Phaser/input/Mouse.ts index ddb27894..bcac228f 100644 --- a/Phaser/input/Mouse.ts +++ b/Phaser/input/Mouse.ts @@ -12,18 +12,17 @@ module Phaser { constructor(game: Game) { - this._game = game; - this.callbackContext = this._game; + this.game = game; + this.callbackContext = this.game; } /** - * Local private reference to game. - * @property _game + * Local reference to game. + * @property game * @type {Phaser.Game} - * @private **/ - private _game: Game; + public game: Game; public static LEFT_BUTTON: number = 0; public static MIDDLE_BUTTON: number = 1; @@ -44,21 +43,32 @@ module Phaser { public mouseMoveCallback = null; public mouseUpCallback = null; + /** + * A reference to the event handlers to allow removeEventListener support + */ + public _onMouseDown; + public _onMouseMove; + public _onMouseUp; + /** * Starts the event listeners running * @method start */ public start() { - if (this._game.device.android && this._game.device.chrome == false) + if (this.game.device.android && this.game.device.chrome == false) { // Android stock browser fires mouse events even if you preventDefault on the touchStart, so ... return; } - this._game.stage.canvas.addEventListener('mousedown', (event: MouseEvent) => this.onMouseDown(event), true); - this._game.stage.canvas.addEventListener('mousemove', (event: MouseEvent) => this.onMouseMove(event), true); - this._game.stage.canvas.addEventListener('mouseup', (event: MouseEvent) => this.onMouseUp(event), true); + this._onMouseDown = (event: MouseEvent) => this.onMouseDown(event); + this._onMouseMove = (event: MouseEvent) => this.onMouseMove(event); + this._onMouseUp = (event: MouseEvent) => this.onMouseUp(event); + + this.game.stage.canvas.addEventListener('mousedown', this._onMouseDown, true); + this.game.stage.canvas.addEventListener('mousemove', this._onMouseMove, true); + this.game.stage.canvas.addEventListener('mouseup', this._onMouseUp, true); } @@ -72,14 +82,14 @@ module Phaser { this.mouseDownCallback.call(this.callbackContext, event); } - if (this._game.input.disabled || this.disabled) + if (this.game.input.disabled || this.disabled) { return; } event['identifier'] = 0; - this._game.input.mousePointer.start(event); + this.game.input.mousePointer.start(event); } @@ -93,14 +103,14 @@ module Phaser { this.mouseMoveCallback.call(this.callbackContext, event); } - if (this._game.input.disabled || this.disabled) + if (this.game.input.disabled || this.disabled) { return; } event['identifier'] = 0; - this._game.input.mousePointer.move(event); + this.game.input.mousePointer.move(event); } @@ -114,14 +124,14 @@ module Phaser { this.mouseUpCallback.call(this.callbackContext, event); } - if (this._game.input.disabled || this.disabled) + if (this.game.input.disabled || this.disabled) { return; } event['identifier'] = 0; - this._game.input.mousePointer.stop(event); + this.game.input.mousePointer.stop(event); } @@ -131,9 +141,9 @@ module Phaser { */ public stop() { - //this._game.stage.canvas.addEventListener('mousedown', (event: MouseEvent) => this.onMouseDown(event), true); - //this._game.stage.canvas.addEventListener('mousemove', (event: MouseEvent) => this.onMouseMove(event), true); - //this._game.stage.canvas.addEventListener('mouseup', (event: MouseEvent) => this.onMouseUp(event), true); + this.game.stage.canvas.removeEventListener('mousedown', this._onMouseDown); + this.game.stage.canvas.removeEventListener('mousemove', this._onMouseMove); + this.game.stage.canvas.removeEventListener('mouseup', this._onMouseUp); } diff --git a/Phaser/input/Touch.ts b/Phaser/input/Touch.ts index 34d2e71c..40aaef77 100644 --- a/Phaser/input/Touch.ts +++ b/Phaser/input/Touch.ts @@ -22,18 +22,17 @@ module Phaser { */ constructor(game: Game) { - this._game = game; - this.callbackContext = this._game; + this.game = game; + this.callbackContext = this.game; } /** - * Local private reference to game. - * @property _game + * Local reference to game. + * @property game * @type {Phaser.Game} - * @private **/ - private _game: Game; + public game: Game; /** * You can disable all Input by setting disabled = true. While set all new input related events will be ignored. @@ -53,22 +52,41 @@ module Phaser { public touchLeaveCallback = null; public touchCancelCallback = null; + /** + * A reference to the event handlers to allow removeEventListener support + */ + public _onTouchStart; + public _onTouchMove; + public _onTouchEnd; + public _onTouchEnter; + public _onTouchLeave; + public _onTouchCancel; + public _documentTouchMove; + /** * Starts the event listeners running * @method start */ public start() { - if (this._game.device.touch) + if (this.game.device.touch) { - this._game.stage.canvas.addEventListener('touchstart', (event) => this.onTouchStart(event), false); - this._game.stage.canvas.addEventListener('touchmove', (event) => this.onTouchMove(event), false); - this._game.stage.canvas.addEventListener('touchend', (event) => this.onTouchEnd(event), false); - this._game.stage.canvas.addEventListener('touchenter', (event) => this.onTouchEnter(event), false); - this._game.stage.canvas.addEventListener('touchleave', (event) => this.onTouchLeave(event), false); - this._game.stage.canvas.addEventListener('touchcancel', (event) => this.onTouchCancel(event), false); + this._onTouchStart = (event) => this.onTouchStart(event); + this._onTouchMove = (event) => this.onTouchMove(event); + this._onTouchEnd = (event) => this.onTouchEnd(event); + this._onTouchEnter = (event) => this.onTouchEnter(event); + this._onTouchLeave = (event) => this.onTouchLeave(event); + this._onTouchCancel = (event) => this.onTouchCancel(event); + this._documentTouchMove = (event) => this.consumeTouchMove(event); - document.addEventListener('touchmove', (event) => this.consumeTouchMove(event), false); + this.game.stage.canvas.addEventListener('touchstart', this._onTouchStart, false); + this.game.stage.canvas.addEventListener('touchmove', this._onTouchMove, false); + this.game.stage.canvas.addEventListener('touchend', this._onTouchEnd, false); + this.game.stage.canvas.addEventListener('touchenter', this._onTouchEnter, false); + this.game.stage.canvas.addEventListener('touchleave', this._onTouchLeave, false); + this.game.stage.canvas.addEventListener('touchcancel', this._onTouchCancel, false); + + document.addEventListener('touchmove', this._documentTouchMove, false); } } @@ -94,7 +112,7 @@ module Phaser { this.touchStartCallback.call(this.callbackContext, event); } - if (this._game.input.disabled || this.disabled) + if (this.game.input.disabled || this.disabled) { return; } @@ -106,7 +124,7 @@ module Phaser { // event.changedTouches = the touches that CHANGED in this event, not the total number of them for (var i = 0; i < event.changedTouches.length; i++) { - this._game.input.startPointer(event.changedTouches[i]); + this.game.input.startPointer(event.changedTouches[i]); } } @@ -124,7 +142,7 @@ module Phaser { this.touchCancelCallback.call(this.callbackContext, event); } - if (this._game.input.disabled || this.disabled) + if (this.game.input.disabled || this.disabled) { return; } @@ -135,7 +153,7 @@ module Phaser { // http://www.w3.org/TR/touch-events/#dfn-touchcancel for (var i = 0; i < event.changedTouches.length; i++) { - this._game.input.stopPointer(event.changedTouches[i]); + this.game.input.stopPointer(event.changedTouches[i]); } } @@ -153,7 +171,7 @@ module Phaser { this.touchEnterCallback.call(this.callbackContext, event); } - if (this._game.input.disabled || this.disabled) + if (this.game.input.disabled || this.disabled) { return; } @@ -205,7 +223,7 @@ module Phaser { for (var i = 0; i < event.changedTouches.length; i++) { - this._game.input.updatePointer(event.changedTouches[i]); + this.game.input.updatePointer(event.changedTouches[i]); } } @@ -230,7 +248,7 @@ module Phaser { // event.changedTouches = the touches that CHANGED in this event, not the total number of them for (var i = 0; i < event.changedTouches.length; i++) { - this._game.input.stopPointer(event.changedTouches[i]); + this.game.input.stopPointer(event.changedTouches[i]); } } @@ -241,14 +259,16 @@ module Phaser { */ public stop() { - if (this._game.device.touch) + if (this.game.device.touch) { - //this._domElement.addEventListener('touchstart', (event) => this.onTouchStart(event), false); - //this._domElement.addEventListener('touchmove', (event) => this.onTouchMove(event), false); - //this._domElement.addEventListener('touchend', (event) => this.onTouchEnd(event), false); - //this._domElement.addEventListener('touchenter', (event) => this.onTouchEnter(event), false); - //this._domElement.addEventListener('touchleave', (event) => this.onTouchLeave(event), false); - //this._domElement.addEventListener('touchcancel', (event) => this.onTouchCancel(event), false); + this.game.stage.canvas.removeEventListener('touchstart', this._onTouchStart); + this.game.stage.canvas.removeEventListener('touchmove', this._onTouchMove); + this.game.stage.canvas.removeEventListener('touchend', this._onTouchEnd); + this.game.stage.canvas.removeEventListener('touchenter', this._onTouchEnter); + this.game.stage.canvas.removeEventListener('touchleave', this._onTouchLeave); + this.game.stage.canvas.removeEventListener('touchcancel', this._onTouchCancel); + + document.removeEventListener('touchmove', this._documentTouchMove); } } diff --git a/Phaser/loader/Loader.ts b/Phaser/loader/Loader.ts index 5b381fee..537fd6f2 100644 --- a/Phaser/loader/Loader.ts +++ b/Phaser/loader/Loader.ts @@ -330,8 +330,6 @@ module Phaser { case 'audio': file.url = this.getAudioURL(file.url); - //console.log('Loader audio'); - //console.log(file.url); if (file.url !== null) { @@ -349,7 +347,6 @@ module Phaser { if (this._game.sound.touchLocked) { // If audio is locked we can't do this yet, so need to queue this load request somehow. Bum. - //console.log('Audio is touch locked'); file.data = new Audio(); file.data.name = file.key; file.data.preload = 'auto'; @@ -363,7 +360,7 @@ module Phaser { file.data.onerror = () => this.fileError(file.key); file.data.preload = 'auto'; file.data.src = file.url; - file.data.addEventListener('canplaythrough', () => this.fileComplete(file.key), false); + file.data.addEventListener('canplaythrough', Phaser.GAMES[this._game.id].load.fileComplete(file.key), false); file.data.load(); } } @@ -425,6 +422,12 @@ module Phaser { */ private fileComplete(key: string) { + if (!this._fileList[key]) + { + throw new Error('Phaser.Loader fileComplete invalid key ' + key); + return; + } + this._fileList[key].loaded = true; var file = this._fileList[key]; @@ -491,6 +494,7 @@ module Phaser { } else { + file.data.removeEventListener('canplaythrough', Phaser.GAMES[this._game.id].load.fileComplete); this._game.cache.addSound(file.key, file.url, file.data, false, true); } break; diff --git a/Phaser/math/GameMath.ts b/Phaser/math/GameMath.ts index c9afa8f4..8392a55e 100644 --- a/Phaser/math/GameMath.ts +++ b/Phaser/math/GameMath.ts @@ -18,7 +18,11 @@ module Phaser { GameMath.sinA = []; GameMath.cosA = []; - for (var i = 0; i < 360; i++) + // Android 4 stock browser bug fix + GameMath.sinA.push(0); + GameMath.cosA.push(0); + + for (var i = 1; i < 360; i++) { GameMath.sinA.push(Math.sin(this.degreesToRadians(i))); GameMath.cosA.push(Math.cos(this.degreesToRadians(i))); diff --git a/Phaser/system/RequestAnimationFrame.ts b/Phaser/system/RequestAnimationFrame.ts index 960bba71..921510ba 100644 --- a/Phaser/system/RequestAnimationFrame.ts +++ b/Phaser/system/RequestAnimationFrame.ts @@ -89,6 +89,11 @@ module Phaser { **/ public isRunning: bool = false; + /** + * A reference to the RAF/setTimeout to avoid constant anonymous function creation + */ + public _onLoop; + /** * Starts the requestAnimatioFrame running or setTimeout if unavailable in browser * @method start @@ -104,12 +109,14 @@ module Phaser { if (!window.requestAnimationFrame) { this._isSetTimeOut = true; - this._timeOutID = window.setTimeout(() => this.SetTimeoutUpdate(), 0); + this._onLoop = () => this.SetTimeoutUpdate(); + this._timeOutID = window.setTimeout(this._onLoop, 0); } else { this._isSetTimeOut = false; - window.requestAnimationFrame(() => this.RAFUpdate(0)); + this._onLoop = () => this.RAFUpdate(0); + window.requestAnimationFrame(this._onLoop); } this.isRunning = true; @@ -148,7 +155,9 @@ module Phaser { this.callback.call(this._game); } - window.requestAnimationFrame((time) => this.RAFUpdate(time)); + this._onLoop = (time) => this.RAFUpdate(time); + + window.requestAnimationFrame(this._onLoop); } @@ -160,7 +169,9 @@ module Phaser { this._game.time.update(Date.now()); - this._timeOutID = window.setTimeout(() => this.SetTimeoutUpdate(), 16.7); + this._onLoop = () => this.SetTimeoutUpdate(); + + this._timeOutID = window.setTimeout(this._onLoop, 16); if (this.callback) { diff --git a/Plugins/Template.js b/Plugins/Template.js index a61f23ae..9fcfd4f8 100644 --- a/Plugins/Template.js +++ b/Plugins/Template.js @@ -1,6 +1,7 @@ var Phaser; (function (Phaser) { /// + /// /** * Phaser - Example Plugin */ @@ -13,31 +14,27 @@ var Phaser; } Example.prototype.preUpdate = /** * Pre-update is called at the start of the update cycle, before any other updates have taken place. + * It is only called if active is set to true. */ function () { }; Example.prototype.postUpdate = /** * Post-update is called at the end of the objects update cycle, after other update logic has taken place. + * It is only called if active is set to true. */ function () { }; Example.prototype.preRender = /** - * Pre-render is called at the start of the object render cycle, before any transforms have taken place. - * It happens directly AFTER a canvas context.save has happened if added to a Camera. - * @param {Camera} camera + * Pre-render is called right before the Game Renderer starts and before any custom preRender callbacks have been run. + * It is only called if visible is set to true. */ - function (camera) { - }; - Example.prototype.render = /** - * render is called during the objects render cycle, right after all transforms have finished, but before any children/image data is rendered. - * @param {Camera} camera - */ - function (camera) { + function () { }; Example.prototype.postRender = /** - * Post-render is called during the objects render cycle, after the children/image data has been rendered. + * Post-render is called after every camera and game object has been rendered, also after any custom postRender callbacks have been run. + * It is only called if visible is set to true. */ - function (camera) { + function () { }; Example.prototype.destroy = /** * Clear down this Plugin and null out references diff --git a/README.md b/README.md index 0bc98b88..ca0966ca 100644 --- a/README.md +++ b/README.md @@ -165,6 +165,9 @@ V1.0.0 * Added Sprite.bringToTop() * Added Stage.disableVisibilityChange to stop the auto pause/resume from ever firing. * Added crop support to the Texture component, so you can do Sprite.crop to restrict rendering to a specified Rectangle without distortion. +* Added references to all the event listener functions so they can be cleanly destroyed. +* Fixed interesting Firefox bug when an audio track ended it fired another 'canplaythrough' event, confusing the Loader. + V0.9.6 diff --git a/Tests/camera fx/mirror.js b/Tests/camera fx/mirror.js index 4a299882..40480ac5 100644 --- a/Tests/camera fx/mirror.js +++ b/Tests/camera fx/mirror.js @@ -22,8 +22,10 @@ // Finally we set the fill color that is put over the top of the mirror effect. mirror.start(0, 400, new Phaser.Rectangle(0, 0, 800, 400), 'rgba(0, 0, 100, 0.7)'); // Experiment with variations on these to see the different mirror effects that can be achieved. - mirror.flipX = true; + //mirror.flipX = true; //mirror.flipY = true; + // The Mirror FX will literally mirror EVERYTHING that was rendered to the camera, in the case of this test it's + // just a single image, but when used on a full game it can look really quite neat. game.add.sprite(0, 0, 'backdrop'); } function update() { diff --git a/Tests/index.php b/Tests/index.php index ff75d675..77883985 100644 --- a/Tests/index.php +++ b/Tests/index.php @@ -136,6 +136,19 @@ if ($state == false && $mobile == false)