我正在学习Phaser,并使用基本按钮进行练习。
例如,只需查看此处的相位器按钮示例即可。
但是,我注意到,如果您按住鼠标单击相位器按钮,将鼠标移离按钮,然后松开鼠标单击,相位器按钮仍然会激活,就好像鼠标就在它上面一样。
您也可以尝试按住鼠标单击相位器按钮,然后将鼠标移出框架。相位器按钮也将激活。
我不确定这是否是 Phaser 中按钮的默认行为,但它不适用于其他 JS 游戏库/引擎,如 Cocos 2D JS。
到目前为止,我已经尝试了以下方法:
- 设置按钮以激活
onInputDown
- 不是真正的首选。 onInputUp
设置另一个函数 - 不会改变行为。
这是我的代码:
this.playButton = this.game.add.button(this.game.world.centerX - 100, this.game.world.centerY + 230, 'ui', this.actionOnClick, this, 'ui_btn_play_U.png', 'ui_btn_play_U.png', 'ui_btn_play_D.png', 'ui_btn_play_U.png');
this.playButton.onInputUp.add(this.startGameplay, this);
actionOnClick() {
}
startGameplay() {
this.clickButton();
this.bgm.stop();
var slideOut = Phaser.Plugin.StateTransition.Out.SlideTop;
slideOut.duration = 1000;
var slideIn = Phaser.Plugin.StateTransition.In.SlideTop;
slideIn.duration = 1000;
this.game.state.start('gameplay' , slideOut );
}
我该怎么做才能改变这种行为?
我很久以前遇到过这个问题,这就是我解决它的方式:您需要将三个事件组合在 InputDown、onInputUp 和 onInputOut。在伪代码中,它将如下所示:
myButton.clicked=false;
When myButton.onInputDown -> myButton.clicked=true
When myButton.onInputOut -> myButton.clicked= false
When myButton.onInputUp -> if (myButton.clicked) then do desired action