How can I create touch controls?

Misael644

Member
Hello! I would like to know how I can make touch controls for my game.

Basically, I wanted to port a game to Android, it's a spaceship game, and I wanted to use buttons on the screen to control the ship, the problem is that I don't know exactly how I can do the touch controls, because I I don't understand much about programming.

It is like this: I made sprites to put in the game room, when I press one of these buttons, the ship will have to go forward, to one side or the other or shoot lasers. I didn't find any tutorial on the internet that could help me.

Do you have any idea how I can program the buttons?
 

chirpy

Member
You might want to take a look at gamemaker's "virtual keys" which map touch areas into keyboard keys. There should be many tutorials.
 

Yal

šŸ§ *penguin noises*
GMC Elder
Virtual keys let you create screen regions that generate button events when tapped / held. They don't actually have graphics, so you need to add new code to draw button sprites as well (this is by design, so that you can draw something matching your style, and make button hitboxes bigger than the visual buttons to make them easier to press).

Mouse events get translated to touch events as well. You can basically only use the left mouse (right-click detection is a bit iffy and some features only work on screens that support them) but if you're more comfortable using the Mouse Events, you could use this approach instead. (Especially for menus and point-and-click games, it's going to be a lot more natural than virtual keys).
 

Misael644

Member
Thank you for your help! Some tutorials that I saw told me to use the virtual_key_add variable. This was not working because I had removed some things from the code. But now I have another problem: I made some sprites so that when I click the buttons, they become brighter, so that I know I pressed them. The problem is that if I press any of the buttons, they will all become brighter. The same goes for the buttons that I put on the title screen, if I press any one, they will all take me to the game room. Can you help me now on this one?
 
Hello! I would like to know how I can make touch controls for my game.

Basically, I wanted to port a game to Android, it's a spaceship game, and I wanted to use buttons on the screen to control the ship, the problem is that I don't know exactly how I can do the touch controls, because I I don't understand much about programming.

It is like this: I made sprites to put in the game room, when I press one of these buttons, the ship will have to go forward, to one side or the other or shoot lasers. I didn't find any tutorial on the internet that could help me.

Do you have any idea how I can program the buttons?
I'm working on a multi-platform solution as we speak. I want to say it will be done tomorrow... but honestly, it will be done when it's done and it'll be done when it's good.

I will make sure to let you know.


There should be many tutorials.
No, there aren't. Well, not any worth mentioning.

Most of what I've seen either doesn't support multi-touch, doesn't move with the view, doesn't scale properly, or all three. :bash:


Thank you for your help! Some tutorials that I saw told me to use the virtual_key_add variable. This was not working because I had removed some things from the code. But now I have another problem: I made some sprites so that when I click the buttons, they become brighter, so that I know I pressed them. The problem is that if I press any of the buttons, they will all become brighter. The same goes for the buttons that I put on the title screen, if I press any one, they will all take me to the game room. Can you help me now on this one?
What you need to do to fix that is use a Finite State Machine, and object inheritance. Don't worry, its not as complicated as it sounds. This coffee-break tutorial explains that concept quite nicely.

 
Last edited:

chamaeleon

Member
Thank you for your help! Some tutorials that I saw told me to use the virtual_key_add variable. This was not working because I had removed some things from the code. But now I have another problem: I made some sprites so that when I click the buttons, they become brighter, so that I know I pressed them. The problem is that if I press any of the buttons, they will all become brighter. The same goes for the buttons that I put on the title screen, if I press any one, they will all take me to the game room. Can you help me now on this one?
Post the code that makes the buttons brighter and code related to moving to another room to make it easier for people to help you. Don't forget to mention what event executes each code shown.
 

kburkhart84

Firehammer Games
I'm working on a multi-platform solution as we speak. I want to say it will be done tomorrow... but honestly, it will be done when it's done and it'll be done when it's good.

I will make sure to let you know.
Yup, we actually are getting close. If it isn't later tonight it will be some time tomorrow. Note that it will be not be using virtual buttons, rather some custom coding in GUI Space.
 

ouzzgame

Member
Hello! I would like to know how I can make touch controls for my game.

Basically, I wanted to port a game to Android, it's a spaceship game, and I wanted to use buttons on the screen to control the ship, the problem is that I don't know exactly how I can do the touch controls, because I I don't understand much about programming.

It is like this: I made sprites to put in the game room, when I press one of these buttons, the ship will have to go forward, to one side or the other or shoot lasers. I didn't find any tutorial on the internet that could help me.

Do you have any idea how I can program the buttons?
Hi, i made a tutorial about GUI Layer and buttons for iOS and Android devices, because it's really hard to find anything about this subject...I tried to share it in the tutorial section but he's not published yet. Probably under moderation because of the links.
Hope this can help! http://ouzzgame.yo.fr/tutorial.html
 

kburkhart84

Firehammer Games
I was working on this twin-stick thing. There seems to be a bug with HTML5 on Android, but it isn't the device, rather the device_mouse_x_to_gui() and device_mouse_y_to_gui() that are the issue. The first touch stays within the GUI space, but the rest of them end up in room space. My project is showing text with the exact values. If you stay within the 1024x768 GUI space, everything works fine, multitouch and all...but as soon as the view scrolls away, only the first touch works. The other touches are detected but since they are outside the range, the joysticks don't pick up on that.

I submitted a ticket with Yoyogames, It is ticket# 168264 in case anyone is interested. I have a feeling that since bug didn't get picked up because most people doing HTML5 on mobile aren't using multi-touch, or are confining things to a small area and so either aren't affected or are not using the GUI versions of the position detection of the devices.
 

Misael644

Member
Uhh... sorry for the long delay in responding, but apparently I managed to fix the problem with the touch controls in my game, and it was very easy. I said that when I pressed on one of the touch controls, instead of just one button changing the sprite, all the button sprites changed, the code previously looked like this:

GML:
switch(buttonid)
{
    case 0:
        if mouse_check_button(mb_left) && position_meeting(mouse_x, mouse_y, id){
            sprite_index = spr_button_turnl_pressed};
        if mouse_check_button_released(mb_left) && position_meeting(mouse_x, mouse_y, id){
            sprite_index = spr_button_turnl};
        
    case 1:
//and the same for the rest of the buttons.
The solution for me to fix all this was just to add the statement of "break;" at the end of each case.
Well, thanks to everyone for helping me with this complication that I had. I hope to count on the help of the community when I need help and I also hope that sometime I will be able to share my game with you.
 
Top