我正在尝试制作一个按钮:
this.add.button(0, 0, 'button-deposit', actionOnClick, this, 1, 0, 2);
但是弹出错误:this.add.button不是一个函数
我做错了什么?
最佳答案
据我所知,Phaser v2 中的按钮类在 Phaser3 中不存在,原因未知。在我的Phaser3 example game在 github 上,我向 Scene
添加了一个 addButton
原型(prototype),以便能够再次使用按钮。
参见下面的代码:
// include code below in separate .js file
// add a button to a scene
Phaser.Scene.prototype.addButton = function(x, y, key, callback, callbackContext, overFrame, outFrame, downFrame, upFrame)
{
// add a button
var btn = this.add.sprite(x, y, key, outFrame).setInteractive();
btn.on('pointerover', function (ptr, x, y) { this.setFrame(overFrame) } );
btn.on('pointerout', function (ptr) { this.setFrame(outFrame) } );
btn.on('pointerdown', function (ptr) { this.setScale(0.9, 0.9) } );
btn.on('pointerup', callback.bind(callbackContext));
return btn;
};
// in a scene you can then do this
var mybutton = this.addButton(100, 100, 'mysprites', this.doButtonAction, this, 'btn_play_hl', 'btn_play', 'btn_play_hl', 'btn_play');
关于javascript - 为什么我无法在 Phaserjs 上创建按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58038366/