Game Development Community

Touch and hold for an allotted amount of time?

by CharlesL · in iTorque 2D · 02/04/2013 (3:42 pm) · 4 replies

I have a quick question - is there a simple way to have a function that will be called only after the sprite has been touched and held for a few seconds? Will I have to use a timer?

Example code would be much appreciated.

Thanks,
- Charles

#1
02/07/2013 (7:00 pm)
Yeah, a timer would be your best bet. This code should work. Make sure you change "sprite" to whatever your class is and change "%this.takeAction()" to whatever function you want to be executed. And change the value of "%timerTick" to whatever period of time you want the user to have to hold on the sprite.

function sprite::onTouchDown( %this, %touchID, %worldPos )
{
    //set timer for 3 seconds
    %timerTick = 3000; 
    %this.setTimerOn( %timerTick );
}

function sprite::onTouchUp( %this, %touchID, %worldPos )
{
    %this.setTimerOff(); //stop the timer if the player releases touch
}

function sprite::onMouseLeave( %this, %modifier, %worldPosition, %clicks )
{
    %this.setTimerOff(); //stop timer if the player's touch leaves the sprite
}

function sprite::onTimer( %this )
{
    %this.setTimerOff(); //stop the timer
    %this.takeAction(); //execute timer action
}
#2
02/09/2013 (2:38 pm)
Cool, Thanks!
#3
02/12/2013 (1:30 pm)
That works perfectly, thank you so much!
#4
02/14/2013 (6:30 pm)
Always happy to help! :)