我想要效果: 点击按钮,播放器正在移动,直到我点击
那时代码的工作方式如下: 我点击按钮,精灵(玩家)正在移动。 但问题是玩家移动了一次,我需要再次点击。
我的左/右移动代码
clickMoveLeft: function()
{
if(!this.clickMoveLeft.clicked)
{
this.player.body.velocity.x += -160;
}
},
clickMoveRight: function()
{
if(!this.clickMoveRight.clicked)
{
this.player.body.velocity.x += 160;
}
},
创建/添加精灵:
this.buttonleft = this.game.add.sprite(0, 600, 'button1');
this.buttonleft.inputEnabled = true;
this.buttonleft.events.onInputDown.add(this.clickMoveLeft, this);
this.buttonleft.clicked = false;
this.buttonright = this.game.add.sprite(320, 600, 'button1');
this.buttonright.inputEnabled = true;
this.buttonright.events.onInputDown.add(this.clickMoveRight, this);
this.buttonright.clicked = false;
答案 0 :(得分:1)
您希望在true
触发时将布尔值设置为mousedown
,然后在false
触发时设置mouseup
。然后,当布尔值为true
时,请调用您的移动。
Example from another question.
编辑:由于您使用的是Phaser,因此您需要mouseDownCallback
和mouseUpCallback
,但它的概念相同。