• 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 Creating virtual buttons for non-platformer game.

T

Tomaato

Guest
Hi there!

After a long break from GameMaker I'm finally getting back into it.
I used to make a lot of simple platformer games based on story rather than complex gameplay. (Mainly because I am not at all a programmer)
Because of the simplicity of the engine and my (very) basic knowledge of the code in GameMaker I got around to making those fairly easily.

Now that I'm back I have a good idea for a game that is a tad different than what I made in the past.
I want to make a game that takes place inside an messaging app. (Think: Emily is Away, but different)

But because I am only familiar with platforming games (Moving your player, scroling the view, changing room/something happens whenever a player sprite touches an object, creating virtual keys linked to keyboard keys), I have no idea how de basics for a game like mine work in GameMaker.

But now for the concrete question to start things off:

Say I have a screen filled with chats, buttons and menus, how do you link the buttons to a function in a good way on a touchscreen?


Like I said, I do have experience creating virtual keys for Android, but only linked to specific keyboard keys. So I would link one to vk_left and set vk_left to move my player left. I read some things about linking them to your left mouse button, but then I still have no clue how to efficiently create the buttons.

I hope I made myself clear!
Thanks in advance,

Tom Hodgson
 

NightFrost

Member
I haven't done touchscreens, but the documentation seems pretty straightforward, you read touches as mouse events.
Code:
if(device_mouse_check_button_pressed(0, mb_left)){
    xx = device_mouse_x(0);
    yy = device_mouse_y(0);
   /// ...
}
Would test for a touch and give you the coordinates where the screen was touched, which you would then compare to bounds of your virtual buttons and react accordingly. The zero in the commands is device zero, or in case of touchscreen, the first touch. If you want to support multiple simultaneous touches you would also check through devices one through four, for each further touch. If you want to support double-tap, you would also check for a mb_right like
Code:
if(device_mouse_check_button_pressed(0, mb_right)){
    xx = device_mouse_x(0);
    yy = device_mouse_y(0);
   /// ...
}
 
T

Tomaato

Guest
I haven't done touchscreens, but the documentation seems pretty straightforward, you read touches as mouse events.
Code:
if(device_mouse_check_button_pressed(0, mb_left)){
    xx = device_mouse_x(0);
    yy = device_mouse_y(0);
   /// ...
}
Would test for a touch and give you the coordinates where the screen was touched, which you would then compare to bounds of your virtual buttons and react accordingly. The zero in the commands is device zero, or in case of touchscreen, the first touch. If you want to support multiple simultaneous touches you would also check through devices one through four, for each further touch. If you want to support double-tap, you would also check for a mb_right like
Code:
if(device_mouse_check_button_pressed(0, mb_right)){
    xx = device_mouse_x(0);
    yy = device_mouse_y(0);
   /// ...
}
Thanks, that kinda helps!

But how can I efficiently set the x and y values for the buttons?
In the past I only had to deal with 5 or so buttons, so I would just look up what their coordinates where. But with 10+ buttons constantly changing is there a way to set the coordinates more efficiently? (Like linking it to the shape of the object?
 

NightFrost

Member
One option would be to run the check in every button object and then compare the x,y position against the button's position. If you give the buttons a mask the size of the sprite (which is the default behavior), you can use bounding box variables (bbox_left etc) to directly read where its edges are. Then, if the touch position is inside the object, run whatever code is necessary.

If you want to optimize, run the pressed check one in the begin step of some control object you've set up and save the coords to globals, then compare against those in buttons' step event.
 
T

TheMatrixHasMe

Guest
Thanks, that kinda helps!

But how can I efficiently set the x and y values for the buttons?
In the past I only had to deal with 5 or so buttons, so I would just look up what their coordinates where. But with 10+ buttons constantly changing is there a way to set the coordinates more efficiently? (Like linking it to the shape of the object?
Each object should have an x and y coordinate and you can reference from those points. If it has a sprite you can use sprite_width and sprite_height to determine the mouse coordinates with that other information. For example,
Code:
if device_mouse_check_button_pressed(0,mb_left){
if device_mouse_x >= x-(sprite_width/2) &&
   device_mouse_x <= x+(sprite_width/2{
if device_mouse_y >= y-(sprite_height/2) &&
    device_mouse_y <= y+(sprite_height/2){
  //button pressed code goes here
|
}
}
Sorry for the crappy indentation work. Anyhow, if you are drawing the objects and not using sprites then just draw with variables such as w for width and h for height and then use those same variables to reference the mouse information like so:
Code:
if device_mouse_x >= x-(w/2)
Hope this helps!
 
Top