Android Creating virtual buttons for non-platformer game.

Discussion in 'Programming' started by Tomaato, Jun 28, 2016.

  1. Tomaato

    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
     
  2. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    1,959
    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);
       /// ...
    }
     
    Tomaato likes this.
  3. Tomaato

    Tomaato 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?
     
  4. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    1,959
    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.
     
    Tomaato likes this.
  5. TheMatrixHasMe

    TheMatrixHasMe Member

    Joined:
    Jun 22, 2016
    Posts:
    176
    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!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice