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
}