Multitouch and holding problem

Ninjazz

Member
Wondering if anybody can help or at least point me in the right direction here.

Been stuck on this for a couple of days now...
The gist of it is a simple mobile game, where a basket moves either left or right on the screen, and is controlled by buttons on either side.
From searching for help and answers I've managed to work out that the best way to do this is usually using a "for" loop and checking input devices and "position_meeting" to see if the coordinates of the input are on the object.
I'll post my code below:
Code:
//Move basket based on dir (for direction) and spd (speed). dir is determined by the buttons. spd is 2 from the creation event.
move = (spd * (dir1 + dir2));
x += move;

//Check Right
for (var i=0;i<2;i+=1;)
    {
        if (device_mouse_check_button(i,mb_left)) && (position_meeting(device_mouse_x(i),device_mouse_y(i),o_RightButton))
            {
                dir2 = 1;
                o_RightButton.image_angle -= rotspd; //rotate button clockwise
  
                //Play Sound (if it's not playing already)
                if !(audio_is_playing(au_sfx))
                    {
                        audio_play_sound(au_sfx,2,true);
                    }
            }
        else
            {
                dir2 = 0;
                audio_stop_sound(au_sfx);
            }
    }

//Check Left
for (var i=0;i<2;i+=1;)
    {
        if (device_mouse_check_button(i,mb_left)) && (position_meeting(device_mouse_x(i),device_mouse_y(i),o_LeftButton))
            {
                dir1 = -1;
               o_LeftButton.image_angle -= rotspd; //rotate button counterclockwise
  
                //Play Sound (if it's not playing already)
                if !(audio_is_playing(au_sfx))
                    {
                        audio_play_sound(au_sfx,2,true);
                    }
            }
        else
            {
                dir1 = 0;
                audio_stop_sound(au_sfx);
            }
    }
The problem I'm having, which I can't figure out, is that when I run the game, the controls don't respond immediately... it's almost like I have to touch both buttons at once to get them to "wake up"... and then it only gets worse. To get the basket to move left or right, I have to first press the button OPPOSITE of where I want to go, and then while holding it, press the button for the other direction... only then will it change direction and go the way I want.
IE: If I want to move the basket left, I first press and HOLD right, and then can press left.
I just can't figure out why it's acting this way, especially since I see the button image rotate correctly when you touch it. (whether the basket moves or not.)

Can anybody explain why or help me work this out? I figure that in the future I will want to be more familiar with multi-touch controls. This is a pretty frustrating first hurdle.
-Ninjazz

EDIT: Forgot to add, this is in the step event of the basket object itself, and my GMS2 Version is 2.2.5.378.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Okay, I confess, I haven't looked at the code, but I do want to comment that from what you say you want to do, you could probably avoid all issues by simply using virtual keys and setting them up on either side of the screen...


PS: I'll now look at your code and edit the reply if I have a suggestion... :p


EDIT: Okay, I think the issue here is that you are checking for two touches in each loop, so if one is detected then the next one cancels it as the "else" is run. You probably want to separate out the code a bit, like this:

GML:
var _rightpressed = false;
for (var i=0;i<2;i+=1;)
    {
    if (device_mouse_check_button(i,mb_left)) && (position_meeting(device_mouse_x(i),device_mouse_y(i),o_RightButton))
        {
        _rightpressed = true;
        }
    }
if _rightpressed
    {
    dir2 = 1;
    o_RightButton.image_angle -= rotspd; //rotate button clockwise
    //Play Sound (if it's not playing already)
    if !(audio_is_playing(au_sfx))
        {
        audio_play_sound(au_sfx,2,true);
        }
    }
else
    {
    dir2 = 0;
    audio_stop_sound(au_sfx);
    }
// Now do the same for the LEFT button
 
Last edited:
Top