简体   繁体   English

如何在Phaser中获得ActivePointer按下的精灵持续时间

[英]how to get activePointer pressed duration of sprite in Phaser

I'm creating pinball game using Phaser Framework . 我正在使用Phaser Framework创建弹球游戏。

When the ball holder is pressed (please check attached screenshot so you have an idea what I mean ball holder), depending on the press speed, it should move the ball around the spiral channel. 按下持球器时(请检查随附的屏幕截图,以便您了解我的意思是持球器),具体取决于按下速度,它将使球绕螺旋通道移动。 So now trying to detect the down pressed duration of the holder. 因此,现在尝试检测支架的按下时间。

Here is my code: 这是我的代码:

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);
}

So pointer.duration is not working and returns 0 . 因此, pointer.duration无法正常工作并返回0

But game.input.activePointer.duration inside update() function is working and returns duration. 但是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);
}

How can I make it work for specific item/sprite? 如何使它适用于特定的物品/精灵? Any ideas please? 有什么想法吗?

在此处输入图片说明

in phaser 3 something like 在移相器3中,类似

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

You can get the time from the scene and calculate it with the downTime from the activePointer. 您可以从场景中获取时间,并通过activePointer中的downTime进行计算。 So, you can try this approach: 因此,您可以尝试以下方法:

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

I came to the conclusion that this is the best approach when you want to get the duration on press down key because there is not duration attribute in activePointer anymore(Phaser3). 我得出的结论是,当您要按下按键时获得持续时间时,这是最好的方法,因为activePointer(Phaser3)中不再有持续时间属性。 So, this code works on Phaser 3 as well. 因此,此代码也适用于Phaser 3。

I hope it helps! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM