Developing a game on the Phaser engine for Typescript .
In State create a class object that is not inherited from the Phaser classes. And I can not kill him in shutdown() .
Thus, with a multiple restart of state , a bunch of objects of this class is formed, which curls the mobile browser.
How to do it correctly?
module MyGame { export class Level_01 extends Phaser.State { buttonRELOAD; preload() { } create() { this.buttonRELOAD = new Buttons(this.game, 'Reload', undefined, undefined, 'RELOAD'); Game.ButtonEnabledTime++; var DEBUGText = this.game.add.text(23, 150, 'BUTTON_ENABLED: ' + Game.ButtonEnabledTime + ' Time', { font: "36px Arial", fill: "#73fe7f", align: "center" }); this.game.state.restart(true, false); } } update() { } shutdown() { //my tryes //this.buttonRELOAD.pendingDestroy = true; //this.buttonRELOAD.destroy(true); //this.buttonRELOAD.DestroyButton(); //this.buttonRELOAD = undefined; //this.buttonRELOAD.destroy(); //this.game.world.remove(this.buttonRELOAD); } } }
module MyGame { export class Buttons { x: any; y: any; constructor(game: Phaser.Game, Name: string, x: any, y: any, GoTo: String) { this.game = game; this.x = x; this.y = y; this.GoTo = GoTo; if (Name === 'Reload') { x = 50; y = 50; let overButtonReload = function () { this.mm_button_enter.play(); } let outButtonReload = function () { } let actionOnClickButtonReload = function () { Game.StateAdd(this.GoTo); //my game state } ButtonReload = this.game.add.button(x, y, 'button_reload', actionOnClickButtonReload, this, '1.png', '1.png', '2.png', '1.png'); this.ButtonReload = ButtonReload; ButtonReload.scale.setTo(1, 1); ButtonReload.onInputOver.add(overButtonReload, this); ButtonReload.onInputOut.add(outButtonReload, this); } public DestroyButton() { //this.ButtonReload.pendingDestroy = true; //this.game.world.remove(this.ButtonReload); this.ButtonReload.destroy(true); this.ButtonReload = null; console.log(this.ButtonReload); } } }
After the main menu, I start this state. in it I generate a button by creating it in another class. For testing, I automatically reboot this state. On the 150th attempt, the sound breaks (probably due to memory overflow), and on the 300th, where everything hangs. I deduce the debugging text to see how many restart attempts have passed.
let obg = { name:'qwa' }; let lin = obg; console.log(lin); setTimeout(() => { console.log(lin); }, 1e3); lin = void(0); console.log(lin);let obg = { name:'qwa' }; let lin = obg; console.log(lin); setTimeout(() => { console.log(lin); }, 1e3); lin = void(0); console.log(lin);- qwabra