移动精灵 onClick 直到它被点击

Move sprite onClick until its clicked

我想要效果: 单击按钮,播放器会移动,直到我真正单击

那时代码的工作方式如下: 我点击按钮,精灵(玩家)正在移动。 但问题是播放器移动了一次,我需要再次点击。

我的移动代码left/right

clickMoveLeft: function()
{
    if(!this.clickMoveLeft.clicked)
    {
        this.player.body.velocity.x += -160;
    }
},

clickMoveRight: function()
{
    if(!this.clickMoveRight.clicked)
    {
        this.player.body.velocity.x += 160;
    }
},

Creating/adding 精灵:

    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;

您想在 mousedown 触发时将布尔值设置为 true,然后在 mouseup 触发时将布尔值设置为 false。然后,当布尔值是 true 时,调用您的动作。

Example from another question.

编辑:由于您使用的是 Phaser,因此您需要 mouseDownCallback and mouseUpCallback,但这是相同的概念。