Jump to content

input detect long press


espace
 Share

Recommended Posts

Hi,

I'm searching to detect a long press and next do some stuff. i have seen that there is possible with "hold" but i can't do this.

I began below a jsfiddle with inputDown with a circle and it works.

Could you help me to do the same but with the notion of long press and delay ?

It could be fine ;)

https://jsfiddle.net/espace3d/x9gevqsg/

var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { create: create, update: update, render: render });

function create() {
    
//when i click on the graphics after a certain delay do some stuff
    var graphics = game.add.graphics(0, 0);
    graphics.beginFill(0xFF0000, 1);
    graphics.drawCircle(300, 300, 100);
    graphics.inputEnabled = true;
    graphics.events.onInputDown.add(onHold,this);

//when i click on the graphics after a certain delay do some stuff
function onHold(graphics, pointer) {
console.log("okay")
}

}

function update() {
}


function render() {
}

 

 

 

 

Link to comment
Share on other sites

Whenever someone clicks a button, add a phaser timer event for however long you want the delay to be, whenever the function from the delay is called, check if the button they are pressing is still being held down, if it is, you know they are long pressing it. If you need to make sure that its the same hold and they havent just pressed the button numerous times, setup a counter for how many times the button has been pressed, pass that into the function, if the number passed into the function is different than the current counter then they have pressed the button more times and do not do the action after the delay.

Link to comment
Share on other sites

Hi Symof and Ralph, thanks both. 

I began an another jsfiddle yesterday night, it's the same approach. I use the pointer.duration to know the time

function getTime(pointer) {

    var lastDuration = pointer.duration;
    console.log(lastDuration)

}

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...