Creating a tap and hold event in Thunkable

A tap and hold event is a useful method that will constantly fire while you are holding your finger on an image sprite or a button. Using an image sprite inside a canvas you could use a method like this to create onscreen controls for a platformer game for example.

Unfortunately such a method doesn't yet exist, although I did suggest it in my post What I’d like to see on Thunkable in 2017 currently the closest method to a touch and hold is the image sprite's dragged event, but this still isn't perfect as it requires you to constantly move your finger for the event to fire.

There is however a way around this and it involves a clock element, so to start this tutorial we'll be adding a clock to our project as well as adding a canvas element with two image sprites, one will be our move button and the other will be our character, you may set the images of these sprites to anything you like.

Also ensure you set the Clocks attributes as follows
Your Clock may have a different number, here I am using Clock1 for animation, Clock2 handles the tap and hold event.

Now we are ready to go into the block view and code the app, in my app I have created a global variable called "isMoving" to detect whether our character is moving or not in our example. Set this variable to False for initialisation.

Next find the clock you will be using for this event in the block view, then add the clock timer event to your project. Inside the clock timer method add a imagesprite.MoveTo method and set its attributes as follows.

Next add in a "Touch Down" event for the image sprite you will be using for movement, in here we'll be enabling the timer when the user touches the button, and as we set previously that timer will fire every 30 ms, this number can be adjusted as needed. Setup your "Touch Down" event as follows.

To finish off our project we lastly need to add a "Touch Up" event, this will fire once the user releases their finger from the screen, as such we will need to disable the timer otherwise it will keep firing. Setup your "Touch Up" as follows.
Setting the Image Sprite picture just sets the character to the default standing sprite, if you are not doing animation this is not required.
Now you should have a fully functional touch and hold event, Thunkable may or may not add this as an event in future, but until they do this is a fully working workaround.





Was this helpful?

Yes No

Comments

Thunkable Components