GMS 2 Drawing interfaces with GUI?

Discussion in 'Programming' started by DirtyLibrary, Sep 11, 2019.

  1. DirtyLibrary

    DirtyLibrary Member

    Joined:
    Jun 25, 2019
    Posts:
    2
    I'm trying to create a menu/interface, which is docked at the bottom of the screen and displays sprites all of the buildings the player can create, along with a "purchase" button next to each of the sprites. The player clicks the button to open up the interface, which pops up, but the buttons are only clickable when the mouse is over a certain position? The map moves with viewports when the cursor is near the edge of the screen, so the objects are not always in the same position.

    Code that displays the objects on screen:
    Code:
    var posX = 500;
    obj1 = instance_create_layer(100, posX, "GUI", obj_bm_placeholder);
    purchase = instance_create_layer(100, posX+30, "GUI", obj_bm_purchase);
    obj1.itemID = "palace";
    
    obj2 = instance_create_layer(210, posX, "GUI", obj_bm_placeholder);
    purchase = instance_create_layer(210, posX+30, "GUI", obj_bm_purchase);
    obj2.itemID = "mineshaft";
    
    obj3 = instance_create_layer(320, posX, "GUI", obj_bm_placeholder);
    purchase = instance_create_layer(320, posX+30, "GUI", obj_bm_purchase);
    obj3.itemID = "watersupply";
    

    Draw GUI event:
    Code:
    var spritename = asset_get_index("spr_construction_" + string(itemID));
    draw_sprite(spritename, 0, x, y);
    
    any advice would be appreciated.
     
  2. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,780
    Don't use room-instance positioning. Use view or screen positions. Or use the GUI event.
     
  3. Binsk

    Binsk Member

    Joined:
    Jun 22, 2016
    Posts:
    582
    To expand on what @TheouAegis said, because the GUI follows the camera you'll need to convert coordinates at some point so they match up.

    1) If you use the EVENT mouse_click or whatever, you'll need to constantly move the object positions so that they match up to the view so that the clicking is in the right spot.

    2) If you do your OWN click detection for your objects you can either 1) Convert the object positions to the camera location (similar to (1)) or convert the mouse coordinates away from camera location. If you do this you can either subtract the view location from your mouse_x/y coordinates or you can instead use window_get_mouse_x/y functions instead.
     
  4. Matt Hawkins

    Matt Hawkins Member

    Joined:
    Jan 29, 2017
    Posts:
    213
    I'd use
    Code:
    device_mouse_x_to_gui(0)
     

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