【问题标题】:Phaser button handler function with parameter带参数的 Phaser 按钮处理函数
【发布时间】:2016-03-17 04:50:22
【问题描述】:

我正在使用 Phaser.js 构建一个 html 游戏。这可能更像是一个一般的 javascript 问题,但我使用 Phaser 作为上下文,所以我尝试使用处理程序设置一个按钮,这是 main.js 中的按钮定义:

var someParam = 2;
var btn = game.add.button(0, 0, 'playButton', actions.handler, this, 1, 0, 1);

在另一个名为 actions.js 的文件中,我定义了处理函数:

var handler = function(someParam) {
    console.log(someParam);
};
module.exports = {
    handler: handler
};

问题是如何将 someParam 传递给处理函数?

【问题讨论】:

    标签: javascript function parameter-passing phaser-framework


    【解决方案1】:

    我不了解 Phaser,但在 JavaScript 中你有两种可能的选择:

    1)

    game.add.button(0, 0, 'playButton', function(){
      actions.handler(someParam);
    }, this, 1, 0, 1);
    

    2)

    game.add.button(0, 0, 'playButton',actions.handler.bind(this,someParam) , this, 1, 0, 1);
    

    选项 2 会将函数绑定到 特定someParam,因此如果它发生更改,它不会在处理程序中更改。

    【讨论】:

    • 没有尝试第二个选项,但第一个选项确实有效。非常感谢!
    猜你喜欢
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    • 2016-02-08
    • 2015-11-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-30
    • 1970-01-01
    相关资源
    最近更新 更多