简体   繁体   中英

how to get activePointer pressed duration of sprite in Phaser

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?

在此处输入图片说明

in phaser 3 something like

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. 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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