• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

instance_create when mouse hover

shmurl

Member
On my intro screen, I have users choose a level. When the mouse hovers over a level, I want an object (obj_points_per_level) to pop up with some info regarding that level. When the mouse isn't hovering over the level the pop up should be destroyed.

I created an obj_points_per_level with different sprites to display the info for each level.

The problem I am encountering is that it will only popup when I mouse over one of the levels and not the other. I imagine the reason is because I wrote that when the mouse isn't hovering over the obj_letters, the obj_points_per_level gets destroyed. Which must be overwriting the code (to create a new obj_points_per_level) when the mouse hovers over the obj_words. I just can't figure out a solution. Is there a more efficient way to do this, perhaps using a switch?

This is the code that I have (I'm trying to keep the code all in one object - obj_choose_level). Note, I will add more code with the other levels once I have the proper code


if popup == false {with (obj_points_per_level)instance_destroy(); popup = true;}; //popup is initialized in the create event

with (obj_letters)
{
if position_meeting(mouse_x,mouse_y,obj_letters) && obj_choose_level.popup = true
{
instance_create(x,y-70,obj_points_per_level);
with (obj_points_per_level){image_index = 1};
}
else if !position_meeting(mouse_x,mouse_y,obj_letters){obj_choose_level.popup = false};
};


with (obj_words)
{
if position_meeting(mouse_x,mouse_y,obj_words) && obj_choose_level.popup == true

{instance_create(x,y-70,obj_points_per_level);
with (obj_points_per_level){image_index = 1};}
else if !position_meeting(mouse_x,mouse_y,obj_words){obj_choose_level.popup = false};
};
 
J

jackhigh24

Guest
game maker has a mouse enter and mouse leave functions for this.
 

Yal

šŸ§ *penguin noises*
GMC Elder
You could use the Mouse Enter and Mouse Leave events for each of the hoverable objects, this lets you split the code up in a more manageable fashion. If you want to keep using the code, I'd use a break statement after each time you DO create a points_per_level object to prevent checks for creating/destroying any more. (See The "Break" Statement). Another idea is to make a parent object for all your hoverable objects, and only destroy the points_per_level objects when there is not an instance of that parent under the mouse position - all the children will count as the parent for the purposes of a check like that.
 

shmurl

Member
@jackhigh Thanks, that is what I am looking for, but I want to do it with code.

@Yal Thanks!
I understand what break does, but I don't understand how to use it (even after viewing the link)
I created an obj_parent_level for the children obj_words and obj_levels.
I placed the following code into the parent step event

if position_meeting(mouse_x,mouse_y,id)
{
instance_create(x,y-70,obj_points_per_level);
with (obj_points_per_level){image_index = 1};
}
else {with (obj_points_per_level)instance_destroy();}
//the above code is giving me the same issue as before, it will only create the instance when hovering over one of the objects

so I changed it to this
if position_meeting(mouse_x,mouse_y,obj_parent_level) //this line is the only line I changed. From id to obj_parent_level.
{
instance_create(x,y-70,obj_points_per_level);
with (obj_points_per_level){image_index = 1};
}
else {with (obj_points_per_level)instance_destroy();}

It does work when hovering over each object, but now whenever mouse hovers over one object it creates the popup (obj_points_per_level) over both obj_words and obj_letters. I only want it to popup over one at a time

EDIT: I'd love to learn how to use the break statement, if you have the patience to explain it ;)
 
Last edited:
Top