SOLVED 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:

Ninjazz

Member
Thank you so much for the help! Also sorry for the late reply, I actually tried to reply a few days ago, but the forum servers apparently went down or something while I was typing, and it didn't go through.

I tried separating out the code and it works exactly how I was hoping. I myself had wondered if using some sort of"trigger" variable could help, but wasn't sure just how to go about it.
There was a bit of an audio issue, but I separated that bit of code from everything else and fixed it. :)

Also thank you for the reference to the virtual keys. I'd not heard of that functionality before, so I took a look and it definitely seems like something useful for me in the future.
Just out of curiosity though, I wonder which method would be better/more efficient? Using the above code method, or using virtual keys?
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Just out of curiosity though, I wonder which method would be better/more efficient? Using the above code method, or using virtual keys?
Well, tbh, I don't think that in most cases it really matter about which of these is more efficient! I'ts not terribly complex code and I personally always suggest people use what's easiest for them to work with first, and only worry about optimising efficiency when they have problems or the code is particularly complex or intensive. That said, I'd say that the VK approach would be the best, simply because once a VK is created you can then use the native Keyboard Events to deal with them, without any loops or collision checks or anything. But like I say, use what's convenient for you!!!
 
Top