gpt4 book ai didi

javascript - 已绘制 Phaser 按钮但未触发单击事件

转载 作者:行者123 更新时间:2023-12-01 02:54:59 25 4
gpt4 key购买 nike

所以这是设置。对于某些上下文,我在 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 具有提升功能,因此 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() {}
}

关于javascript - 已绘制 Phaser 按钮但未触发单击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46752165/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com