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

Checking mouse button only if clicked on a particular object

Discussion in 'Programming' started by KnightFury, Oct 26, 2016.

  1. KnightFury

    KnightFury Guest

    So I have a surface and I'd like to be able to change colors with which everything is drawn in-game.
    I have a button which is supposed to make the cursor draw with green:
    Code:
    if mouse_check_button_released(mb_left)
    {
    surface_set_target(global.surface)
    draw_set_color(c_green)
    draw_line_width(pastx, pasty, _x, _y, 5)
    surface_reset_target()
    }
    But whatever I try to do with the mouse event (making it pressed, checked, etc.), the code applies instantly when I enter the room and the mouse is checked globally as well (instead of when I click on the sprite of the objtect that should be changing the color). What could I be missing here?

    Will be grateful for any pieces of advice
    Thanks.
     
  2. Alexx

    Alexx Member

    Joined:
    Jun 21, 2016
    Posts:
    441
    Try:
    Code:
    if mouse_check_button_released(mb_left) && position_meeting(mouse_x, mouse_y, id)
     
  3. Darn it! you beat me to it!!!
     
    Last edited by a moderator: Oct 26, 2016
  4. KnightFury

    KnightFury Guest

    hm.. color doesn't change at all now >_<
     
  5. try that code in an object that follows the mouse
    with
    && collision_point(mouse_x,mouse_y,obj_?)
    and in a step
    x = mouse_x
    y = mouse_y
     
    Last edited by a moderator: Oct 26, 2016
  6. Alexx

    Alexx Member

    Joined:
    Jun 21, 2016
    Posts:
    441
    I may have misunderstood what you're trying to do.

    You have a button, that when clicked you want to change the line drawn in another object or the same one?
     
  7. KnightFury

    KnightFury Guest

    Nope..
    I must have put my thoughts incorrectly. I wanted the button to simply change the drawing color, just like in paint, photoshop, or any other program
     
  8. KnightFury

    KnightFury Guest

  9. && (place_meeting(mouse_x, mouse_y, object_ofchoice))
     
  10. KnightFury

    KnightFury Guest

    nope :(
    maybe there's something in my code that changes the color?
     
  11. well it might help or totaly not, but I do remember some while back when changing rooms with releasing the mouse button it reacts on the next thing that needs to be handled. (change room by left mouse button click release and react like next room am in the some menu becouse it was triggered with the same release button)
     
  12. Still need help?
     
  13. DukeSoft

    DukeSoft Member

    Joined:
    Jun 20, 2016
    Posts:
    783
    Position meeting takes the bounding box into account. Does your object have a sprite / bounding box?

    Also, you could use the "mouse left click" event in the object itself and run the code there.

    If there's no sprite / bounding box the event won't work either (there's nothing to click on) but you can check it yourself like so;

    Code:
    if (
       mouse_x > x && 
       mouse_y > y && 
       mouse_x < x + width && 
       mouse_y < y + height
    ) {
        //your code here
    }
    
     
  14. KnightFury

    KnightFury Guest

    yes

    yes, it has a sprite

    Already did that (with each of the mouse events), doesn't work

    what is it supposed to do?
     
  15. DukeSoft

    DukeSoft Member

    Joined:
    Jun 20, 2016
    Posts:
    783
    In what event did you put the code?

    Because "if mouse_check_button_released(mb_left)" will never return true in the "mouse pressed" event for example.

    If you put this code in the draw event, i'm quite sure it should work.

    How do you draw the global.surface?

    EDIT:
    And whats the surface size / values of _x, _y, pastx and pasty?

    If your surface is for example 256x256, and the x and y values are over 256x256 (e.g. mouse_x) then it draws outside of the surface.
     
  16. Oh! Is it in a create event?
     

  17. Hello?
     
  18. Alexx

    Alexx Member

    Joined:
    Jun 21, 2016
    Posts:
    441
    Put simply, you want the player to click somewhere to select a drawing colour?

    I could do a cool example if you need it.
     
  19. KnightFury

    KnightFury Guest

    Step. If I put it in the draw event, then the sprite for changing colors doesn't appear at all
    Code:
    x = room_width/2;
    global.surface_color=c_white;
    global.surface=surface_create(300, 300);
    surface_set_target(global.surface);
    draw_clear_alpha(global.surface_color,1);
    surface_reset_target();
    pastx=mouse_x;
    pasty=mouse_y;
    _x=mouse_x;
    _y=mouse_y;
    Yes, please!
     
  20. Alexx

    Alexx Member

    Joined:
    Jun 21, 2016
    Posts:
    441
  21. The Reverend

    The Reverend Member

    Joined:
    Sep 8, 2016
    Posts:
    558
    I think @CatOnMushroom had the solution already. I don't think you need to fix this with global mouse and stuff:
    And trhere's a function you could use when changing rooms to solve this:
    Code:
    mouse_clear(mb_left); // or whatever button needs to be cleared
    After this code the mouse wont return "pressed" or "down" until the mouse button is released.
     

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