【问题标题】:Phaser button drawn but click event not triggering移相器按钮已绘制,但单击事件未触发
【发布时间】:2017-10-15 05:56:46
【问题描述】:

所以这是设置。对于某些上下文,我在 Vue.js 中运行它。

      preload () {
        this.load.image("button", button)
      },
      create () {
        this.game.stage.backgroundColor = "#ffa500";

        var button = this.game.add.button(this.game.world.centerX, this.game.world.centerY, 'button', test, this, 0);
        button.anchor.set(0.5)

        var test = function() {
          console.log('hi')
        }
      },
      update () {
      }

当我单击图像时,没有任何反应。不知道从这里开始。

【问题讨论】:

    标签: javascript phaser-framework


    【解决方案1】:

    JavaScript 有提升,所以test 的声明会被移到顶部,但这并不意味着赋值也会。所以你的代码基本上可以运行

    var button = undefined;
    var test = undefined;// hoisted
    button = this.game.add.button(this.game.world.centerX, 
    this.game.world.centerY, 'button', undefined, this, 0);
    button.anchor.set(0.5)
    test = function() {
        console.log('hi')
    }
    

    如果您希望test 有值,请将 test 的分配移到按钮上方,或使用内部函数

    create() {
        function test() {}
    }
    

    【讨论】:

    • 还是不行。我什至尝试用匿名函数替换测试,但仍然没有输出:\
    • 感谢您的跟进,但我已切换到 pixi.js。我已经得到了点击工作大声笑。无论如何,我会给你一个赞成票。
    猜你喜欢
    • 2015-10-18
    • 1970-01-01
    • 2010-10-21
    • 2013-01-17
    • 2012-03-14
    • 2011-12-02
    • 2013-10-14
    • 2018-11-07
    • 1970-01-01
    相关资源
    最近更新 更多