Jump to content

Phaser Typescript, Can't use private method?


xuchun
 Share

Recommended Posts

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

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

  • 2 weeks later...
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

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...