• Hey! Guest! The 40th (!!!) GMC Jam will take place between February 25th, 12:00 UTC to March 1st 12:00 UTC. Why not join in this very special anniversary jam! Click here to find out more!

GMS 2.3+ How to make multiple Joystick?

Hello everyone
I follow this tutorial to make Virtual Joystick.

but when i put two joysticks on room screen.
I find it can't multiple touch, any idea how to solve it??
Thank you very much

my step code:
GML:
var move = device_mouse_check_button(0, mb_left);
var fingerX = device_mouse_x(0);
var fingerY = device_mouse_y(0);

var dirToStable = point_distance(fingerX,fingerY,joystickStableX,joystickStableY);
var dirToFinger = point_direction(fingerX,fingerY,joystickStableX,joystickStableY) + 180;
 
Last edited:

kupo15

Member
the second device mouse should be a 1 instead of a 0 device_mouse_check_button(1,mb_left) What you did was have two device 0 on the screen controlling the same one.
 
the second device mouse should be a 1 instead of a 0 device_mouse_check_button(1,mb_left) What you did was have two device 0 on the screen controlling the same one.
Thank you for reply.
Sorry, still not work with the second joystick
The following is my full project, anyidea how to make that?? thank you very much

 

kupo15

Member
Thank you for reply.
Sorry, still not work with the second joystick
The following is my full project, anyidea how to make that?? thank you very much

Got it, I've gotten it to partially work but not fully, I can tell you the direction to go into to try and get it working.

That function device_mouse relates to the number of touches occurring:

mouse_x(0) will only register when at least touch is happening
mouse_x(1) will only register when at least two touches are happening

This means that you can't simply assign 0 and 1 independently like I initially thought. This means you need to come up with a system where when you click on one joystick, say the left one, the left one is reading from device(0) and then when you hold down the right one, it reads from device(1). But then if you let go of the left one, the right one now needs to be reading from device(0) instead. Then when the left is re held down, it needs to be reading from device[1]

This reverses when the right is pressed down first. Its not simply a matter of assigning slot 0 to left and 1 to right but having them able to swap numbers on the fly

I suggest creating a controller object that handles the inputs. Have it keep track of a variable called "available_slot" or something to that effect and have each joystick object check that variable to determine which slot it should be referencing for the mouse_x and y. So if available_slot is 0 that means as soon as a joystick presses down, its active slot becomes 0, then increments the available slot counter by 1. That way when the other joysticks presses down, it now pulls 1 as its active slot...then increments by 1.

Lastly, in the controller object when any device_mouse_released is triggered, -1 from both joysticks active slot. This means that they will go negative which is fine. Use -1 to mean the joystick is not active and make sure to move your joystick pointdirection...etc... code inside the if move code. Var move should only be true if active_slot >= 0, not -1

This is the conclusion I came to while at work...didn't get it completely working but I'm pretty sure this is the way to handle it.
 

kburkhart84

Firehammer Games
The way I've handled it is to not assign touches to joysticks right away. Each joystick has a specific space that belongs to it(a square or circle, however you prefer). Each joystick would loop through ALL touches(I'd recommend 5 just in case). If there is a touch in the vicinity of that joystick area, check where it is in relation to the center, and that gets you the direction it is tilted in.

As far as I know, there is no way to know for sure which touch ID is going to be where on the screen, so you are better off just checking every frame where the touches are, and each joystick will handle itself separately.
 
Top