how to get activePointer pressed duration of sprite in Phaser

╄→尐↘猪︶ㄣ 提交于 2019-12-01 12:04:55

问题


I'm creating pinball game using 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.

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


回答1:


in phaser 3 something like

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



回答2:


You can get the time from the scene and calculate it with the downTime from the activePointer. 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). So, this code works on Phaser 3 as well.

I hope it helps!



来源:https://stackoverflow.com/questions/47357205/how-to-get-activepointer-pressed-duration-of-sprite-in-phaser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!