Phaser.io按钮回调未触发(TypeScript,VSCode)

时间:2015-07-26 00:18:02

标签: javascript typescript visual-studio-code phaser-framework

我已经按照本网站上列出的模板进行了完美的工作:http://divillysausages.com/2015/06/09/using-phaser-with-visual-studio-code/

然后我尝试在create方法中为游戏添加一个按钮:

class SimpleGame
{
    game :Phaser.Game;

    constructor()
    {
        this.game = new Phaser.Game(800, 600, Phaser.AUTO, 'content', {preload: this.preload, create: this.create, update: this.update});
    }

    preload()
    {
        this.game.load.spritesheet('button', 'assets/button-round-a.png', 64, 64);
    }

    create()
    {
        var button = this.game.add.button(0, 0, 'button', this.onButtonPress, this);
    }

    update()
    {
    }

    onButtonPress()
    {
        console.log('Here!');
    }
}

window.onload = () => {
    var game = new SimpleGame();
}

按钮确实按预期显示,但按下它什么都不做。生成的JS是这样的:

var SimpleGame = (function () {
    function SimpleGame() {
        this.game = new Phaser.Game(800, 600, Phaser.AUTO, 'content', { preload: this.preload, create: this.create, update: this.update });
    }
    SimpleGame.prototype.preload = function () {
        this.game.load.spritesheet('button', 'assets/button-round-a.png', 64, 64);
    };
    SimpleGame.prototype.create = function () {
        var button = this.game.add.button(0, 0, 'button', this.onButtonPress, this);
    };
    SimpleGame.prototype.update = function () {
    };
    SimpleGame.prototype.onButtonPress = function () {
        console.log('Here!');
    };
    return SimpleGame;
})();
window.onload = function () {
    var game = new SimpleGame();
};

我已经尝试过搞乱这个并做其他奇怪的事情,但后来它调用了函数undefined。基本上,回调根本没有被触发。我试图在方法调用中插入预期的功能,但它确实有效,但我不想将其保留在那里。任何帮助都会很大:D:D:D。

1 个答案:

答案 0 :(得分:0)

固定! 而不是

this.onButtonPress

我必须使用

SimpleGame.prototype.onButtonPress