GML how to check if mouse is touching object

so i'm trying to set up my GUI and what i want to do is if the mouse is over game start then there will be a border around it so you know that what you have selected
but when ever you start the game it just flashes a bunch. here is the code it is in a step event of the object for the start button
GML:
if(position_meeting(mouse_x, mouse_y, spr_gui_game_start))
{
object_set_sprite(1, obj_gui_game_start)
}
else
{
object_set_sprite(0, obj_gui_game_start)
}
i'v also tried
Code:
if(position_meeting(mouse_x, mouse_y, obj_gui_game_start))
{
object_set_sprite(1, obj_gui_game_start)
}
else
{
object_set_sprite(0, obj_gui_game_start)
}
also should i make one object for gui or just make a new one for every new button?
thanks for the help
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Moderator
Use instance_position(). And also, check out the differences between the different collision functions and how they work as you are definitely using them wrong... so read up on the functions and how to use them before going any further. ;)


Also, to set the sprite on an instance simply set its "sprite_index" variable. If you set the OBJECT sprite (as you are doing) you will be changing the object properties, not the instance properties, so nothing in the game will change until you actually create a new instance of that object. So, yeah, also read up on the difference between objects and instances!

 

TheouAegis

Member
Make your button sprite be one single sprite with 2 frames -- one frame without the border, one frame with the border. Inside obj_gui_game_start, you can just do
Code:
image_index = position_meeting(mouse_x, mouse_y, id);
Then after you do that, read everything Nocturne told you to read, because you have a lot to wrap your head around.
 
Use instance_position(). And also, check out the differences between the different collision functions and how they work as you are definitely using them wrong... so read up on the functions and how to use them before going any further. ;)


Also, to set the sprite on an instance simply set its "sprite_index" variable. If you set the OBJECT sprite (as you are doing) you will be changing the object properties, not the instance properties, so nothing in the game will change until you actually create a new instance of that object. So, yeah, also read up on the difference between objects and instances!

the sprite is changing
also i'l go read about collisions

Make your button sprite be one single sprite with 2 frames -- one frame without the border, one frame with the border. Inside obj_gui_game_start, you can just do
Code:
image_index = position_meeting(mouse_x, mouse_y, id);
Then after you do that, read everything Nocturne told you to read, because you have a lot to wrap your head around.
it is one sprite
 
it is i just searched how to change sprite but i probably should have searched how to change sprite frame.
also how do you find threads that you posted if nobody has posted in them for a long time?
 
.......
also how do you find threads that you posted if nobody has posted in them for a long time?
We do this on others posts :
Untitled.png
But on ourselves, you can also click your username on the top right of the screen and choose "Your Content", besides doing the same.
 
Last edited:

FrostyCat

Member
is there anyway you can set this up in Drag and Drop? I prefer visual scripting because that is how I learn things.
There is, you just have to use to the equivalent actions.
With the more conventional form:
1626816303821.png

With TheouAegis's short form on post #3:
1626816035190.png

I suggest that if you are going to use Drag and Drop, you should learn how to save yourself with the Drag and Drop reference and common sense. Drag and Drop has next to no traction among experienced responders because it is annoying to support (i.e. opening up GM, taking screenshots, making sure all the details are visible, etc. versus just typing out code from memory), and most of them simply do not have the time or will to cater to that kind of extra hassle.
 
Just to clarify, "id" is the game object that I will choose? And You are saying that I should stick with GML? It would make sense that some functions are not used with code blocks but Drag and Drop is the kind of programming that someone with a brain like mine can understand.
 

FrostyCat

Member
Just to clarify, "id" is the game object that I will choose?
No, it is the built-in read-only variable id.
And You are saying that I should stick with GML? It would make sense that some functions are not used with code blocks but Drag and Drop is the kind of programming that someone with a brain like mine can understand.
If at all possible, you should stick with GML. Besides, with GMS 2 drag-and-drop, quite often you will be inputting GML expressions into the action fields anyways.
 
Top