Swiping Mechanic

Hello, I am new to Gdevelop 5, or making games in general. My goal is to make a game for IOS.

I was trying to create a movement for the player in which swiping up, down, left, or right will move the player in a certain number of pixels in that direction. I attempted this with the condition of “Left Key is pressed” which is followed by an act of “Do -130 to the X position of Player”. However, this doesn’t work as it isn’t an instant movement or change in position. I tried “Left Key is released” and this works, but I was hoping if there was an alternative to this since there is a slight delay.

My MAIN concern, however, is how would this be on an IOS app. My Player’s movements depend on the arrow keys being released on my keyboard. How do I get a swipe mechanic?

The important bit is that you need to use the mouse and touch events instead of the keyboard.

If you want touch controls only like touch left side of the screen move left, touch right to move right or on-screen buttons, an example is included with GDevelop for how to implement touch controls.

If you want swipe like swipe left to move left, swipe right to move right, you need to use variables to store the position of the initial touch and current touch and expressions to get their position, and compare the variables. If you are new to variables and expressions, I recommend to read the docs on the wiki first.

In a nutshell:

Get initial position
Condition:
If Left mouse button or touch is down
Trigger Once

Action:
Do = MouseX() to variable touchInitialX
Do = MouseY() to variable touchInitialY

Get current position
Condition:
no condition

Action:
Do = MouseX() to variable touchCurrentX
Do = MouseY() to variable touchCurrentY

Compare them to see if there was a swipe or not
Condition:
If mouse Left mouse button or touch is NOT held
Trigger Once

—Sub-Condition
If touchInitialX different than touchCurrentX

—Action:
we swiped left or right do what you want to do and then

—Sub-Condition:
if touchInitialY different than touchCurrentY

—Action:
we swiped up or down, do whatever you want to and then

However, the problem with the above solution is that, currently it is considered a swipe even if we slowly move our finger just a little bit in one direction.
To make sure it was an explicit swipe You might also want to use a timer to check the elapsed time between touching the screen and releasing it. If it happened within say 1 second, it could have been a swipe but if it was 2+ second, it was not swipe. Than you might also want to check the distance. If the user moved it finger only certain amount of pixels like say 200 pixels within <= 1 second only than you consider it a swipe. If it took > 1 second or the length is < 200 pixels than you don’t consider it a swipe. It is something you need to experiment with to get it right that feels natural that feels like a proper swipe.

Yeah, some swipe conditions could be useful to have :wink:

4 Likes

Thank you for replying! That looks rough, but I’ll try it out Thanks!

Hey I’m trying to figure out how to do the swiping mechanic and am having a hard time. Do you think you could upload a picture of the events? Thank you!


This is what I have. Clicking my mouse button doesn’t work. How do I actually test if swiping works because if I swipe on my touchpad of my laptop, it does nothing.

I think it should be two separated event.

touchInitialX > touchCurrentX THEN Do - 130…
touchInitialX < touchCurrentX THEN Do + 130…

Both can’t be true at the same time.