我正在尝试做一个按钮:
this.add.button(0, 0, 'button-deposit', actionOnClick, this, 1, 0, 2);
但是弹出错误消息:this.add.button不是函数
我在做什么错了?
答案 0 :(得分:3)
AFAIK,由于未知原因,在Phaser3中不存在类似Phaser v2中的按钮类。在我在github上的Phaser3 example game中,我向addButton
添加了一个Scene
原型,以便能够再次使用按钮。
请参见下面的代码:
// 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');