• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

Checking mouse button only if clicked on a particular object

K

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.
 

Alexx

Member
Try:
Code:
if mouse_check_button_released(mb_left) && position_meeting(mouse_x, mouse_y, id)
 
S

SyntheticStorm9

Guest
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:

Alexx

Member
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?
 
K

KnightFury

Guest
try
&& collision_point(mouse_x,mouse_y,obj_?) instead
Nope..
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?
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
 
C

CatOnMushroom

Guest
&& (place_meeting(mouse_x, mouse_y, object_ofchoice))
 
C

CatOnMushroom

Guest
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)
 

DukeSoft

Member
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
}
 
K

KnightFury

Guest
Still need help?
yes

Position meeting takes the bounding box into account. Does your object have a sprite / bounding box?
yes, it has a sprite

Also, you could use the "mouse left click" event in the object itself and run the code there.
Already did that (with each of the mouse events), doesn't work

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;
what is it supposed to do?
 

DukeSoft

Member
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.
 

Alexx

Member
Put simply, you want the player to click somewhere to select a drawing colour?

I could do a cool example if you need it.
 
K

KnightFury

Guest
In what event did you put the code?
Oh! Is it in a create event?
Step. If I put it in the draw event, then the sprite for changing colors doesn't appear at all
How do you draw the global.surface?
And whats the surface size / values of _x, _y, pastx and pasty?
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;
Put simply, you want the player to click somewhere to select a drawing colour?
I could do a cool example if you need it.
Yes, please!
 
I think @CatOnMushroom had the solution already. I don't think you need to fix this with global mouse and stuff:
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)
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.
 
Top