xuchun Posted February 23, 2017 Share Posted February 23, 2017 Hello all, I am new to Phaser and Typescript, I have code below, very simple, but doesn't work, when running in VS 2015, the browser says: "Unhandled exception at line 26, column 9 in http://localhost:15726/control.js 0x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'doSomething'" but If I remove the code "this.doSomething();" from preload() method, everything works fine what did I do wrong? I don't understand thanks /// <reference path="scripts/phaser.d.ts" /> class demo { private game: Phaser.Game; constructor() { this.game = new Phaser.Game(500, 500, Phaser.AUTO, "content", { create: this.create, preload: this.preload, update: this.update }); } private doSomething(): void { } private update(): void { } private preload(): void { this.doSomething(); } private create(): void{ } } window.onload = () => { new demo(); }; Link to comment Share on other sites More sharing options...
rroylance Posted February 24, 2017 Share Posted February 24, 2017 It's because 'this' in the context of preload is not your instance of your demo class; 'this' in the context of preload is the Phaser.Game. You will have the same issue with both create, and update as well... you could get around that by binding your functions when you set them; /// <reference path="scripts/phaser.d.ts" /> class demo { private game: Phaser.Game; constructor() { this.game = new Phaser.Game(500, 500, Phaser.AUTO, "content", { create: this.create.bind(this), preload: this.preload.bind(this), update: this.update.bind(this) }); } private doSomething(): void { } private update(): void { } private preload(): void { this.doSomething(); } private create(): void{ } } window.onload = () => { new demo(); }; Link to comment Share on other sites More sharing options...
xuchun Posted March 9, 2017 Author Share Posted March 9, 2017 On 2/24/2017 at 3:59 PM, UncleAcid said: It's because 'this' in the context of preload is not your instance of your demo class; 'this' in the context of preload is the Phaser.Game. You will have the same issue with both create, and update as well... you could get around that by binding your functions when you set them; /// <reference path="scripts/phaser.d.ts" /> class demo { private game: Phaser.Game; constructor() { this.game = new Phaser.Game(500, 500, Phaser.AUTO, "content", { create: this.create.bind(this), preload: this.preload.bind(this), update: this.update.bind(this) }); } private doSomething(): void { } private update(): void { } private preload(): void { this.doSomething(); } private create(): void{ } } window.onload = () => { new demo(); }; dude, thank you so much, it works rroylance 1 Link to comment Share on other sites More sharing options...
Recommended Posts