• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Android [Solved] Touch control

yvodlyn

Member
Hello everyone again, I'm sorry for my questions.

I created touch control with mouse input in game maker. when a test the game on my android phone, and when my first finger touch the screen, I can control the player one and when I touch the screen with the second finger, I can control the other player. But when I release the first finger, I can't control the second player. So To control the second player, I need to control the first player before. What I want, is to control the second player with controlling the first one.

to help me, here the code:

For the first player:
/// position with mouse
if device_mouse_x(0){
if(device_mouse_x(0) > 512){
y = clamp(device_mouse_y(0), 140, 702);
}
}

For the second player:
/// position with mouse
if device_mouse_x(1){
if(device_mouse_x(1) < 512){
y = clamp(device_mouse_y(1), 140, 702);
}
}

thxt for your help!!!!
 

Binsk

Member
There is no way for the device to know which finger is who's. It's impossible in that regard.

However, looking at your code it seems that one player controls on one side of the screen while the other controls via the opposite side. Instead of checking a specific device for a player, check both devices for both players. You can determine which "finger" is which player's based on the position rather than the device ID.

Now, this leads to the possibility of one player reaching to the other side and cheating. You will have to figure out a way to solve this as best you can as it really can't be avoided. Perhaps something like checking if both devices are on the same side of the screen and preventing control in that case, or something similar.

Hope that helps.
 

yvodlyn

Member
There is no way for the device to know which finger is who's. It's impossible in that regard.

However, looking at your code it seems that one player controls on one side of the screen while the other controls via the opposite side. Instead of checking a specific device for a player, check both devices for both players. You can determine which "finger" is which player's based on the position rather than the device ID.

Now, this leads to the possibility of one player reaching to the other side and cheating. You will have to figure out a way to solve this as best you can as it really can't be avoided. Perhaps something like checking if both devices are on the same side of the screen and preventing control in that case, or something similar.

Hope that helps.
Your suggestion worked, but it didn't multitouch
 

Tornado

Member
Code:
You can do something like this in player object:

- initialize the variable in create event
mousePressedDeviceIndex = -1;

then in step event or wherever

- first use device_mouse_check_button_pressed

for (var i = 0; i <= 4; i++) {
    if (device_mouse_check_button_pressed(i, mb_left) && position_meeting(device_mouse_x(i),device_mouse_y(i), id))
    {
        mousePressedDeviceIndex = i;
    }
}

That way in the variable mousePressedDeviceIndex you will hold the information which device is pressed on the player object

- then you use device_mouse_check_button for moving that player

for (var i = 0; i <= 4; i++) {
    if (device_mouse_check_button(i, mb_left) && i==mousePressedDeviceIndex)
    {
        move the player according to device_mouse_x(i) and device_mouse_y(i);
    }
}

then on checking for release, reset the variable to -1

for (var i = 0; i <= 4; i++) {
    if (device_mouse_check_button_released(i, mb_left))
    {
        if (i==mousePressedDeviceIndex) {
             mousePressedDeviceIndex = -1;
        }
    }
}
i just wrote this quickly just to point you in this direction.
something like that. 

I you dont want to have all that code in players object, then you can use global variables to remember which device has been held down for which player:
global.player1device
global.player2device
 
Last edited:

yvodlyn

Member
Code:
You can do something like this in player object:

- initialize the variable in create event
mousePressedDeviceIndex = -1;

then in step event or wherever

- first use device_mouse_check_button_pressed

for (var i = 0; i <= 4; i++) {
    if (device_mouse_check_button_pressed(i, mb_left) && position_meeting(device_mouse_x(i),device_mouse_y(i), id))
    {
        mousePressedDeviceIndex = i;
    }
}

That way in the variable mousePressedDeviceIndex you will hold the information which device is pressed on the player object

- then you use device_mouse_check_button for moving that player

for (var i = 0; i <= 4; i++) {
    if (device_mouse_check_button(i, mb_left) && i==mousePressedDeviceIndex)
    {
        move the player according to device_mouse_x(i) and device_mouse_y(i);
    }
}

then on checking for release, reset the variable to -1

for (var i = 0; i <= 4; i++) {
    if (device_mouse_check_button_released(i, mb_left))
    {
        if (i==mousePressedDeviceIndex) {
             mousePressedDeviceIndex = -1;
        }
    }
}

something like that.

I you dont want to have all that code in players object, then you can use global variables to remember which device has been held down for which player:
global.player1device
global.player2device

woooooooowwwwwww....Thanks a lot...That will permit me to complete my game.
 

Tornado

Member
cool.
I just added above that I put this together very quickly so you might need to correct/tweek it a bit as it is not a working version, but just wanted to give you the idea.
 
Top