• 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] Touchscreen: Grabbing/Moving/Letting-Go Objects On-Screen by Touch

A

Adam Martinez

Guest
Hello!

I'm making a game for Android. I want the player to be able to touch and drag objects around the screen without having to simulate a mouse click by tapping the screen. Is there a function for detecting whether or not the player is touching the screen? If not, is there I way I can allow the player to just touch and drag objects?

Thank you!

Edit: I changed the title so the thread may be easier to find by search.
 
Last edited by a moderator:

Roderick

Member
On mobile, a touch IS a mouse click.

mouse_check_button tells you if the player is currently touching the screen.
mouse_check_button_pressed tells you if the player touched the screen this frame.
mouse_check_button_released tells you if the player stopped touching the screen.

If you want to support multi-touch, use device_mouse_check_* instead.
 
A

Adam Martinez

Guest
This post was helpful. I'm currently still trying to solve my particular issue.

I should say, the problem is that when the player is not touching the screen, the "mouse cursor" remains in the same place, so when I touch a different object, the first object I touched is still grabbed and moved around instead. I worked around this by using an object that shares the mouse cursor's position, and making it's position move off-screen whenever the screen isn't being touched, but I'm currently unable to allow for position change of these objects relative to the finger movement. Instead, the objects only work when the position is the same as this "mouse" object.

I will post a solution or more clear detail here soon >_<;; Thank you!
 
A

Adam Martinez

Guest
"same as this mouse object" as in... anywhere I touch an object that I want to drag, it will immediately jump to the center of my finger. My previous code for allowing relative position change doesn't work. I will post more soon, hopefully a solution!
 

Roderick

Member
Use mouse_check_button_released to see when the player stops touching the screen, and drop the object then, just like you would on PC when the player releases the mouse button while dragging an object.
 
A

Adam Martinez

Guest
Here is the solution I worked out to allow touch and drag on-screen.

I'm sure this has been done many times in many different ways but I ran into two problems of my own: The object I touch would jump to the middle of where my finger is on the screen instead of becoming "moveable", and moving relative to my finger; and because the mouse cursor doesn't move or disappear just because the player lifts his or her finger off the screen, I was unable to grab new objects once I had grabbed another object, because the first object would be "grabbed" again and jump to the position of my finger on the screen as I tried to grab another object.

Thanks again for your help, Roderick!

obj_mouse


Create
hand_full = false

Step
if mouse_check_button(mb_left) = true {
x = mouse_x
y = mouse_y​
}

obj_object_to_touch_and_move


Create
x_diff = 0
y_diff = 0
grabbed = false

Step

//The difference in position between the object to be moved and the touch input is stored in two variables. This is so you can "grab" objects on-screen from anywhere on the object, and the motion of the grabbed object can stay relative to the motion of the player's finger, instead of jumping to some position directly under the player's finger.

if grabbed = false {
x_diff = obj_mouse.x - x
y_diff = obj_mouse.y - y​
}

//Objects the player touches are "grabbed", and these objects follow the player's finger as long as the screen is touched.

if place_meeting(x,y,obj_mouse)
&& mouse_check_button(mb_left) = true
&& grabbed = false
&& obj_mouse.hand_full = false {
grabbed = true
obj_mouse.hand_full = true
audio_play_sound(sfx_grab, 1, false)​
}

if grabbed = true
&& (obj_mouse.x != -100)
&& (obj_mouse.y != -100) {
x = obj_mouse.x - x_diff
y = obj_mouse.y - y_diff​
}

//"Grabbed" objects are "let go" when the player lifts his or her finger off the screen. The "mouse" object is then moved to some arbitrary position out of the view (-100, -100) until the player touches the screen again at some different position. (I believe) This is necessary, or no matter where the player touches next, the last object "let go" will be "grabbed" again because the position of the device cursor doesn't move or disappear just because the player stops touching the screen.

if mouse_check_button(mb_left) = false {
grabbed = false
obj_mouse.hand_full = false
if obj_mouse.x != -100
&& obj_mouse.y != -100 {
audio_play_sound(sfx_drop, 1, false)​
}​
obj_mouse.x = -100
obj_mouse.y = -100
}
 
Top