【问题标题】:Phaser.io button callback not triggering (TypeScript, VSCode)Phaser.io 按钮回调未触发(TypeScript,VSCode)
【发布时间】:2015-10-16 09:56:53
【问题描述】:

我已经按照这个网站上列出的模板进行了操作,效果很好: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();
};

我试过搞砸这个并做其他奇怪的事情,但后来它调用了未定义的函数。基本上,回调根本不会被触发。我试图在方法调用中插入预期的功能,它可以工作,但我不想保留它。任何帮助都会很大:D :D :D。

【问题讨论】:

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


    【解决方案1】:

    已修复! 而不是

    this.onButtonPress
    

    我不得不使用

    SimpleGame.prototype.onButtonPress
    

    【讨论】:

      猜你喜欢
      • 2016-07-11
      • 2023-03-26
      • 1970-01-01
      • 2014-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多