Multi-touch controls for mobile devices

C

Cdiddysir

Guest
So I am trying to make a mobile game, however I am having trouble with the touch controls. I have an on screen analog stick on the left side of the screen.
Here's the input code for that.


Code:
// create event


/// @description Initialize Virtual Stick
//gui scale
scale = ob_game.window_scale;
//change to customize stick
max_displacement    = 24; //how far the stick can move from center position
stick_radius        = 16; //size of area user can touch to move
//do not change
interacting    = false;
stick_x        = x;
stick_y        = y;
input_h        = 0; //used to store the current input by the user between -1.0 and 1.0
input_v        = 0;


//step event
var gui_mouse_x = device_mouse_x_to_gui(0);
var gui_mouse_y = device_mouse_y_to_gui(0);

if(device_mouse_check_button(0, mb_left))
{
    if(point_distance(x * scale, y * scale, gui_mouse_x, gui_mouse_y) <= max_displacement * scale)
    {
        interacting = true;
    }
}
else if(!device_mouse_check_button(0, mb_left))
{
    interacting = false;
}

And i have virtual buttons on the right of the screen, here's the code for that.

Code:
// create event

/// @description Initialize Virtual Button
//gui scale
scale    = ob_game.window_scale;
sprite    = sp_button;
active    = false;
text    = "";


//step event
var gui_mouse_x = device_mouse_x_to_gui(0);
var gui_mouse_y = device_mouse_y_to_gui(0);

if(device_mouse_check_button_pressed(0, mb_right))
{
    var spw, sph, x1, x2, y1, y2;

    spw = sprite_get_width(sprite);
    sph = sprite_get_height(sprite);

    x1 = (x - (spw / 2)) * scale;
    x2 = (x + (spw / 2)) * scale;
    y1 = (y - (sph / 2)) * scale;
    y2 = (y + (sph / 2)) * scale;
 
    if(point_in_rectangle(gui_mouse_x, gui_mouse_y, x1, y1, x2, y2))
    {
        active = true;
    }
}
else
{
    active = false;
}
For some reason, no matter what I do, or what device I set the buttons to, I can't use the virtual stick and the virtual button at the same time. I would have to stop my character first and then use the virtual button for it to register. It works on my PC just fine, but when I test it on my android it's like that. Any help would be appreciated.
 
T

Timothy

Guest
You have no way of knowing which "device" it will be... so you must check them all (break early if you find it). "device" can just be thought of as a touch id or sorts.
 
C

Cdiddysir

Guest
I tried setting it to different devices. I set the analog stick to device 0 and the buttons to device 1 as separate ID’s however it still didn’t work.
 
T

Timothy

Guest
I tried setting it to different devices. I set the analog stick to device 0 and the buttons to device 1 as separate ID’s however it still didn’t work.
The "ids" or "devices" are not something that is static... they change. You need to do something like
Code:
active = false; // its assumed false, so put it here
var n = 5; // replace with how ever many devices/ touches you want to check
for (var i = n; i >= 0; --i)
{
   var gui_mouse_x = device_mouse_x_to_gui(i);
   var gui_mouse_y = device_mouse_y_to_gui(i);

   if(device_mouse_check_button_pressed(i, mb_right))
   {
      var spw, sph, x1, x2, y1, y2;

      spw = sprite_get_width(sprite);
      sph = sprite_get_height(sprite);

      x1 = (x - (spw / 2)) * scale;
      x2 = (x + (spw / 2)) * scale;
      y1 = (y - (sph / 2)) * scale;
      y2 = (y + (sph / 2)) * scale;

      if(point_in_rectangle(gui_mouse_x, gui_mouse_y, x1, y1, x2, y2))
      {
          active = true;
          break;
      }
   }
}
You would need to do the same sort of thing for your analog stick
 
Top