如何在Phaser中获得activePointer压缩精灵的持续时间

时间:2017-11-17 18:38:39

标签: javascript canvas touch phaser-framework

我正在使用Phaser Framework创建弹球游戏。

当按下球架时(请检查附带的屏幕截图,以便您知道我的意思是球架),根据压机速度,它应该围绕螺旋通道移动球。所以现在试图检测持有人的压下持续时间。

这是我的代码:

var ballButton;
ballButton = game.add.sprite(196, 100, 'ballHolder');
ballButton.inputEnabled = true;
ballButton.events.onInputDown.add(inputDownAction, this);

function inputDownAction(ballButton, pointer) {

    /* returns 0 */
    console.log( pointer.duration);
}

因此pointer.duration无效并返回0

但是update()函数内的game.input.activePointer.duration正在运行并返回持续时间。

if (game.input.activePointer.duration > 200 && game.input.activePointer.duration < 500){
    console.log('first range '+game.input.activePointer.duration);
}else if(game.input.activePointer.duration > 500 && game.input.activePointer.duration < 700){
    console.log('second range '+game.input.activePointer.duration);
}else if(game.input.activePointer.duration > 700){
    console.log('third range '+game.input.activePointer.duration);
}

如何让它适用于特定的项目/精灵?有什么想法吗?

enter image description here

2 个答案:

答案 0 :(得分:1)

在移相器3中类似

function update(){
 var duration = 0
 if( this.input.activePointer.isDown ){
  duration++;
 }
}

答案 1 :(得分:0)

您可以从场景中获取时间,并通过activePointer中的downTime进行计算。因此,您可以尝试这种方法:

function update(time, delta){
    console.log(time - window.game.input.activePointer.downTime);
}

我得出的结论是,这是当您想要按下按键时获得持续时间的最佳方法,因为activePointer(Phaser3)中不再有持续时间属性。因此,此代码也适用于Phaser 3。

希望对您有帮助!