Z
Zarim
Guest
Hello I was planning on clicking a sprite from my GUI but i seems ii'm a little bit stuck here can anyone please help me?
var mousex = device_mouse_x_to_gui(0); // GUI position of mouse's x coordinate
var mousey = device_mouse_y_to_gui(0); // GUI position of mouse's y coordinate
// top left corner
var xx = 0; // x position of the GUI element (insted of 0)
var yy = 0; // y position of the GUI element (insted of 0)
var ww = 50; // width of the GUI element (insted of 50)
var hh = 50; // height of the GUI element (insted of 50)
if (point_in_rectangle(mousex, mousey, xx, yy, xx + ww, yy + hh)) {
// Check for mouse input
if (mouse_check_button_pressed(mb_left)) {
// What to happen when clicked
show_message("click.");
}
}
Thank you soo much for the reply . But there was a missing argument :OYou can use:
and check for collision with the GUI element with:Code:var mousex = device_mouse_x_to_gui(0); // GUI position of mouse's x coordinate var mousey = device_mouse_y_to_gui(0); // GUI position of mouse's y coordinate
Code:// top left corner var xx = 0; // x position of the GUI element (insted of 0) var yy = 0; // y position of the GUI element (insted of 0) var ww = 50; // width of the GUI element (insted of 50) var hh = 50; // height of the GUI element (insted of 50) if (point_in_rectangle(mousex, mousey, xx, xx + ww, yy + hh)) { // Check for mouse input if (mouse_check_button_pressed(mb_left)) { // What to happen when clicked show_message("click."); } }
He forgot an argument in the point_in_rectangle function - something which you could easily solve yourself by reading the error message and checking the manual.Oh sorry, i din't saw the variable. the only problem is the missing argument
point_in_rectangle(mousex, mousey, xx, xx + ww, yy + hh)
Maybe because you should have this in the Step event:Yeah I found the issue, but there is just one thing I can't get. It dosent work. I've put it in the creation code where I draw my gui but it seems that it wont work
mousex = device_mouse_x_to_gui(0); // GUI position of mouse's x coordinate
mousey = device_mouse_y_to_gui(0); // GUI position of mouse's y coordinate
He should have the whole point_in_rectangle section in the Step Event too - so it's checked every step.Maybe because you should have this in the Step event:
Code:mousex = device_mouse_x_to_gui(0); // GUI position of mouse's x coordinate mousey = device_mouse_y_to_gui(0); // GUI position of mouse's y coordinate
Yeah I guess this wouldn't have happened if I was more specific... you are drawing the GUI in creation code... It's quite obvious that you need to check for something in a STEP EVENT (or in an alarm for better performance)... It's working now right?Yeah I found the issue, but there is just one thing I can't get. It dosent work. I've put it in the creation code where I draw my gui but it seems that it wont work
gui_h = display_get_gui_height();
gui_w = display_get_gui_width();
counter = 0;
width = 0;
height = 0;
emitEmoji = 0;
mousex = device_mouse_x_to_gui(0); // GUI position of mouse's x coordinate
mousey = device_mouse_y_to_gui(0); // GUI position of mouse's y coordinate
mousex = device_mouse_x_to_gui(0); // GUI position of mouse's x coordinate
mousey = device_mouse_y_to_gui(0); // GUI position of mouse's y coordinate
if (point_in_rectangle(mousex,mousey,width - 8,height - 8,width + 8,height + 8)) {
if (mouse_check_button_pressed(mb_left)) {
emitEmoji = counter;
}
}
if showEmojiGrid {
counter = 0;
for (var ii = 0;ii < 5;ii++) {
for (var i = 0;i < 10;i++) {
width = 16 + (((gui_w - 16)/ 10) * i);
height = (gui_h /2) + (ii * 16);
draw_sprite(sprite_index,counter, width,height);
draw_rectangle(width-8,height -8,width+8,height+8,true)
draw_text(50,70,string(mousex));
draw_text(50,90,string(mousey));
draw_text(50,50,string(emitEmoji));
counter ++;
};
};
};
This is the same code that I posted originally?Hey, check this
GML:if showEmojiGrid { counter = 0; for (var ii = 0;ii < 5;ii++) { for (var i = 0;i < 10;i++) { width = 16 + (((gui_w - 16)/ 10) * i); height = (gui_h /2) + (ii * 16); draw_sprite(sprite_index,counter, width,height); draw_rectangle(width-8,height -8,width+8,height+8,true); draw_text(50,70,string(mousex)); draw_text(50,90,string(mousey)); draw_text(50,50,string(emitEmoji)); counter ++; } } }
Thank you so muchYou can use:
and check for collision with the GUI element with:Code:var mousex = device_mouse_x_to_gui(0); // GUI position of mouse's x coordinate var mousey = device_mouse_y_to_gui(0); // GUI position of mouse's y coordinate
Edit: fixed the missing argument (sorry i'm blind, jk)Code:// top left corner var xx = 0; // x position of the GUI element (insted of 0) var yy = 0; // y position of the GUI element (insted of 0) var ww = 50; // width of the GUI element (insted of 50) var hh = 50; // height of the GUI element (insted of 50) if (point_in_rectangle(mousex, mousey, xx, yy, xx + ww, yy + hh)) { // Check for mouse input if (mouse_check_button_pressed(mb_left)) { // What to happen when clicked show_message("click."); } }