• 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!

room_goto by mouse click on object

L

Larf

Guest
I feel like I'm going to look very stupid, I have a object I want to use as a button. So I put it in the room and the object has this code in the step event:

if mouse_check_button_pressed(mb_left) and position_meeting(mouse_x, mouse_y, object)
{
room_goto(2);
}

But when I left click on the object, nothing happens, does someone know what's wrong?

Thank you for your time!
 
L

lord_berto

Guest
trying changing "object" to "self" ? so :


if mouse_check_button_pressed(mb_left) and position_meeting(mouse_x, mouse_y, self)
{
room_goto(2);
}

and make sure your button object has a collision mask.

if its still not working, try this for debug:

instead of room_goto(2); say end_game() if the game ended that is because your button works but maybe your "room_goto" is not correctly functioning
 
L

Larf

Guest
Thanks for your response!

I've tried both, it has a collision box and I already tried self.

Also, game_end doesn't work, I also tried to remove the second condition, so its just mouse_check_button_pressed(mb_left), and the room_goto(2) worked. So I'm completely lost.
 
Have you tried using the code in mouse event? E.g. Mouse Left Press Event.
If you add there, all you likely need is
to add "room_goto(2)". And when pressed in the game, it'll go there.
 
L

lord_berto

Guest
Thanks for your answer! But still it doesn't work.
ok lets test if its the collision then...
do,
if position_meeting(mouse_x, mouse_y, self)
{
game_end();
}
if that doesnt exit the game... then,
is your game using physics??
 
E

Ephemeral

Guest
self is depreciated.

Try
Code:
if (mouse_check_button_pressed(mb_left))
{
    if (position_meeting(mouse_x, mouse_y, id)) room_goto(2);
}
If that still doesn't work
try using the Mouse Event
The Mouse Event fires under exactly the conditions you want anyway. (There is a separate Global Mouse Event that tracks all clicks, not just clicks within the collision mask FYI)
 
L

Larf

Guest
Thank you again for your answers.

Both of these didn't work, but if that helps, other rooms of my game use physics, though not the current room and not the object button.
 

samspade

Member
Thank you again for your answers.

Both of these didn't work, but if that helps, other rooms of my game use physics, though not the current room and not the object button.
The code Ephemeral posted is correct and I use it or a version of it all the time. If it doesn't work, then something is off with your object.

Doe your object have a sprite and more importantly, does it have a mask and if so how is the mask set up? Is your object 'in' the GUI layer (are you using the draw GUI event to see where it is)?
 
L

Larf

Guest
Ok, I was double checking things, playing around with the room and the settings, I deleted and created the button object again and I created a new room. I discovered that for some reason. the button only works when I put it near the center of the room, when It's in the corner of the room, like it was before, it doesn't work. WHAT IS HAPPENING
 

samspade

Member
Ok, I was double checking things, playing around with the room and the settings, I deleted and created the button object again and I created a new room. I discovered that for some reason. the button only works when I put it near the center of the room, when It's in the corner of the room, like it was before, it doesn't work. WHAT IS HAPPENING
How is the mask set up and how are you drawing it with a draw event or a draw gui event?

One thing you might want to try is drawing the bounding box in the draw event. This will tell you where the game thinks it is at least:

Code:
///draw event
draw_self();
draw_set_color(c_red);
draw_rectangle(bbox_left, bbox_top, bbox_right, bbox_bottom, true);
You could also put the following code in a step event:

Code:
if (position_meeting(mouse_x, mouse_y, id)) {
    show_debug_message("Here"); 
}
Then check the output window to see if the debug message shows up. Those two things together should show you were the object is and when your mouse is over it.
 
L

Larf

Guest
Thank you again!

The box works for both the center of the screen and the corner, but the "here" only works when the object is in the center. I hope that helps!

For the collision mask, since it's the sprite is basically just a square, I just use the sprite editor and put "automatic" and "rectangle". Also, I just draw it normally by putting it in the room manually and then putting it's coordinates in the create event.
 

samspade

Member
Thank you again!

The box works for both the center of the screen and the corner, but the "here" only works when the object is in the center. I hope that helps!

For the collision mask, since it's the sprite is basically just a square, I just use the sprite editor and put "automatic" and "rectangle". Also, I just draw it normally by putting it in the room manually and then putting it's coordinates in the create event.
When you say the box works does that mean that the red rectangle showing the mask shows up around the sprite where you think it should be? Or just that it shows up somewhere. Or to put it another way are you saying that for both the center of the room and the edge you are clicking within the red rectangle but the debug message only shows up in the center?

That would indeed be odd. It really sounds like a mismatch between position which most often occurs when you're putting things on the GUI layer as that uses a different coordinate system. But if you're drawing it normally, I don't know why it would happen. Are you using views, cameras, messing with the application surface at any point?

The only other thing I can think is add in a draw event that draws a circle at mouse_x and mouse_y right after the draw rectangle and see if that's where it should be.

draw_circle(mouse_x, mouse_y, 15, true);
 
L

Larf

Guest
Sorry I wasn't clear enough before. The red rectangle perfectly "encircles" the button, is exactly where the box is and is the perfect size. It's that when I would put the box away from the center of the screen, the red ractangle would still be perfect but the "here" would not show up.

BUT with the circle thing, you made another discovery! When the mouse is near the center of the room, the circle is following the mouse, but the more I move my mouse away from the center, the more the circle "lags" behind the mouse (It doesn't lag behind, it's like the circle travels less distance than the mouse as the mouse moves away from the center, or to put it in other words, it stops before reaching the mouse, and the distance between the center of the circle and the mouse is bigger the more the mouse is away from the button).

So when I put the button in the corner, and move my mouse so it's on the button, the circle is not on the button, it's more near the center!

And when I put my mouse even further away (so that the mouse doesn't touch the button), but the circle is on the button, IT WORKS! THE "here" SHOW UP! YOU ARE A GENIUS!
 

samspade

Member
Sorry I wasn't clear enough before. The red rectangle perfectly "encircles" the button, is exactly where the box is and is the perfect size. It's that when I would put the box away from the center of the screen, the red ractangle would still be perfect but the "here" would not show up.

BUT with the circle thing, you made another discovery! When the mouse is near the center of the room, the circle is following the mouse, but the more I move my mouse away from the center, the more the circle "lags" behind the mouse (It doesn't lag behind, it's like the circle travels less distance than the mouse as the mouse moves away from the center, or to put it in other words, it stops before reaching the mouse, and the distance between the center of the circle and the mouse is bigger the more the mouse is away from the button).

So when I put the button in the corner, and move my mouse so it's on the button, the circle is not on the button, it's more near the center!

And when I put my mouse even further away (so that the mouse doesn't touch the button), but the circle is on the button, IT WORKS! THE "here" SHOW UP! YOU ARE A GENIUS!
I'm not sure I would call that 'working' but it shows that the coordinates system of the mouse and your room are not aligned. The only reason I can think of for this being the case is that one is using GUI or maybe some scaling issue with the camera, but that doesn't really seem to fit here.

If this were happening to me I would do the following things:
  • Think through my code. Am I messing with the application surface, views, or cameras anywhere? Am I messing with mouse position anywhere? Directly setting it for example. Control + Shift + F will let you do a global search which might be worth doing for any functions which could affect those things.
  • Create a new room in this project and try to duplicate it with as few objects as possible. After being able to duplicate it with as few objects as possible, start deleting/commenting out code until the problem stops.
  • Create an entirely new project and see if you can duplicate the issue there.
 
Top