【问题标题】:Phaser button doesn't call a functionPhaser 按钮不调用函数
【发布时间】:2015-07-05 21:36:53
【问题描述】:

我在 Phaser 中创建了一个按钮:

//---------------------------------Boot
module.exports = {

 init: function() {
    closeButton = game.add.button(w/1.47, h/1.272, 'close', this.closeProf, this);
    closeButton.fixedToCamera = true;
    closeButton.inputEnabled = true;
 },

 closeProf: function() {
    alert('aaa')
 }
};

Module.exports - 这是 Browserify。

在主文件中有这段代码:

//---------------------------------Main
window.game = new Phaser.Game((h > w) ? h : w, (h > w) ? w : h, Phaser.CANVAS, 'game', {render:render});

var ship;
var cursors;
var sun1;
var rc;
var space;

var profile;
var closeButton, holdLeft, holdRight, avaProfile;

game.state.add('Boot', require('./states/boot.js'));
game.state.add('Play', require('./states/play.js'));
game.state.start('Boot');

如何让按钮调用函数?

【问题讨论】:

    标签: jquery function button phaser-framework


    【解决方案1】:

    它看起来像是在自己调用closeProf。在创建按钮之前,添加变量var that = this。 不要添加上下文,这意味着您正在调用this.this.closeProf

    您的按钮代码应如下所示:

    module.exports = {
    
      init: function() {
        var that = this;
        closeButton = game.add.button(w/1.47, h/1.272, 'close', that.closeProf);
        closeButton.fixedToCamera = true;
        closeButton.inputEnabled = true;
     },
    
     closeProf: function() {
        alert('aaa')
     }
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-05
      • 2015-05-14
      • 1970-01-01
      • 2015-07-15
      • 1970-01-01
      • 2020-11-11
      • 2014-12-22
      • 2013-02-11
      相关资源
      最近更新 更多