1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

GM:S 1.4 How to make a button

Discussion in 'Programming' started by prithvidiamond, Jul 25, 2016.

  1. prithvidiamond

    prithvidiamond Member

    Joined:
    Jun 21, 2016
    Posts:
    39
    How to make button such that if a sprite exists at certain coordinates and the button is clicked it repeats a certain number of scripts again.

    I tried this code but it didn't work:

    if (mouse_check_button_pressed(mb_left) && point_in_rectangle(mouse_x, mouse_y, bbox_left, bbox_top, bbox_right, bbox_bottom) && sprite_get_xoffset(spr_fr_wm) = 80 && sprite_get_yoffset(spr_fr_wm) = 320 && sprite_exists(spr_fr_wm) && fruit = 0)
    {
    points += 1;
    scr_draw_obj1();
    scr_click_check();
    }

    Also there are 16 such buttons that need be created so if this extra bit information is required, then you have it. Another thing note is that there are 16 different sprites, each 1 appears in each button with a draw event which is randomized.

    All suggestions and help is really appreciated!


    PRITHVIDIAMOND.
     
  2. Micah_DS

    Micah_DS Member

    Joined:
    Jul 19, 2016
    Posts:
    511
    What do you mean by "it didn't work"? Are you getting an error or does it simply do nothing at all?

    If "scr_draw_obj1()" and "scr_click_check()" are supposed to be executing scripts, then it should be:
    script_execute(scr_draw_obj1);
    script_execute(scr_click_check);

    ( EDIT: whoops, apparently both ways are acceptable, sorry... I'll never get used to some of these redundancies in GML... )

    If that's not the issue, my advice is to break up your code a bit and debug each part to figure out what isn't meeting the criteria. Like maybe doing something like this and monitoring "triggered" to see if it becomes true:
    Code:
    if point_in_rectangle(mouse_x, mouse_y, bbox_left, bbox_top, bbox_right, bbox_bottom) {
        triggered = true;
    }
    You could also add a few more parenthesis, just to ensure it's being read properly, like so:
    Code:
    if mouse_check_button_pressed(mb_left) && point_in_rectangle(mouse_x, mouse_y, bbox_left, bbox_top, bbox_right, bbox_bottom) && (sprite_get_xoffset(spr_fr_wm) == 80) && (sprite_get_yoffset(spr_fr_wm) == 320) && sprite_exists(spr_fr_wm) && (fruit == 0)
     
    Last edited: Jul 25, 2016
  3. Siolfor the Jackal

    Siolfor the Jackal Member

    Joined:
    Jun 21, 2016
    Posts:
    788
    I've only made buttons a couple of times but I tend to make them generic objects.
    I make a button object with a default sprite(animated so you can see it depressed) and all the basic things like if(mouse_pressed) etc.
    Then when I make my menus I have a controller object create them and pass to them different sprites, text if they need text on them and an action or script that they need to perform.
     
    prithvidiamond likes this.
  4. prithvidiamond

    prithvidiamond Member

    Joined:
    Jun 21, 2016
    Posts:
    39
    \

    Could please explain how its exactly done in brief ( I am not experienced and I a noob :( )
     
  5. zendraw

    zendraw Member

    Joined:
    Jun 20, 2016
    Posts:
    1,376
    i think what he means is that he cant press the button, so i use this code, very simple and light.
    if (abs(mouse_x-x)<32 && abs(mouse_y-y)<32)
    {
    if (mouse_check_button_pressed(mb_left))
    {
    //do stuff
    }
    }

    abs() means that the number you get from mouse_x-x will aways be positive and it checks simply if its close enought to the origin point of the button which must be in the center of the button for this to work.
    if your origin is on the top left corner of the sprite then use if (mouse_x-x>0 && mouse_x-x<32)
    but the origin in the center is easyer.
     
  6. prithvidiamond

    prithvidiamond Member

    Joined:
    Jun 21, 2016
    Posts:
    39
    Thanks to all, will definitely try all suggestions and hopefully one of them work!

    PRITHVIDIAMOND.
     
  7. Aura

    Aura Guest

    @K-BitCRUSH: You don't need to necessarily use script_execute(); scr_draw_obj1() does the same thing as script_execute(scr_draw_obj1).

    @prithvidiamond: You're performing odd operations there. Could you explain what you expect it to do? Also, what do you mean by "doesn't work"? Perhaps you want to do operations depending on the image that the button instance has got. In that case:

    Code:
    if (mouse_check_button_pressed(mb_left)) {
       var button = instance_position(x, y, obj_button);
       if (button != noone) {
          if (button.image_index == 0) { //use sprite_index if you are using multiple sprites instead of a single sprite with multiple images
             //Do something
          }
       }
    }
    
    ...should suffice. But since it's pretty unclear what you're trying to do, so it might not to be possible for anybody to give an accurate answer. Information in that regard would help a lot.
     
    Micah_DS likes this.

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