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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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