V
VansiusProductions
Guest
Hi guys,
I want to get the coordinates of an instance when it is clicked by mouse. How do I do that?
I want to get the coordinates of an instance when it is clicked by mouse. How do I do that?
var inst = instance_position(mouse_x, mouse_y, obj_instance);
if (inst != noone) {
inst_x = inst.x;
inst_y = inst.y;
}
Didn't work, nothing showed up. Still tryingChecks for a certain instance at mouse coordinates, then gets the x and y values.Code:var inst = instance_position(mouse_x, mouse_y, obj_instance); if (inst != noone) { inst_x = inst.x; inst_y = inst.y; }
Im using the inst.x and inst.y in his code to create an instance:@shadowspear1 's code should work, you just need to add some draw_text code to your draw event in order to display the inst_x and inst_y variables.
instance_create(inst.x,inst.y,obj_selection)
Where are you running that code?but it's not showing up
In a script. called by:Where are you running that code?
with (obj_selection) {scr_selection()}
Post more of your code, please. It's hard to tell what the problem is unless we can see how you're using the code that was suggested.
if (global.elementSelectedA != 0) {
var inst = instance_position(mouse_x, mouse_y, obj_element);
if (inst != noone) {
inst_x = inst.x;
inst_y = inst.y;
visible = 1;
instance_create(inst.x,inst.y,obj_selection)
}
}
...and where is that script called? I am going to assume in a mouse press or release event.In a script. called by:
In a script. called by:
Code:with (obj_selection) {scr_selection()}
That's basically it nothing more.Code:if (global.elementSelectedA != 0) { var inst = instance_position(mouse_x, mouse_y, obj_element); if (inst != noone) { inst_x = inst.x; inst_y = inst.y; visible = 1; instance_create(inst.x,inst.y,obj_selection) } }
It's called in a mouse left button released event. It has a 70x70 pixel sprite, full image bounding box. Yes, I mentioned that i tested it. Yes I used show_debug_message and it does...and where is that script called? I am going to assume in a mouse press or release event.
What is your obj_selection like? Does it have a sprite?
Check obj_element's collision mask and bounding box.
Is it even running this code? Does it get past "global.elementSelectedA != 0"? Use show_debug_message to see if it does.
var inst = instance_position(mouse_x, mouse_y, obj_element);
if (inst != noone) {
show_debug_message('coords are '+string(inst.x)+','+string(inst.y))
}
I called the script from obj_element. obj_selection Still not showing up. The coords are showing up for meAre you using a global mouse event? Because if you are calling it in an object other than obj_element, it won't execute unless you are in the calling object's bounding box (i.e. never in this case).
EDIT: With just two objects and this code in obj_selector's global left pressed code, I get the coordinates properly displayed.
Code:var inst = instance_position(mouse_x, mouse_y, obj_element); if (inst != noone) { show_debug_message('coords are '+string(inst.x)+','+string(inst.y)) }
No I am not, since @whale_cancer said that obj_element should call it, since the code is already in obj_element, I didn't use with statement. yes, I placed an instance in the room so it would exist in the room. I don't think it will overlay cause their positions are different. The code for left button released event:You're calling it from a with statement correct? Does an instance of obj_selection already exist? The new instance could be overlaying the old one, so you don't see it perhaps? Is there any other code you haven't posted from the mouse left button released event?
if (global.elementHover != 0) {
if (global.elementSelectedA == 0) {
global.elementSelectedA = global.elementHover;
global.elementHover = 0
}
else {
if (global.elementSelectedB == 0) {
global.elementSelectedB = global.elementHover;
global.elementHover = 0
}
}
scr_selection()
}
scr_comboElements()
Yes it's visible. No they hold integer values, 0 as nothing, other numbers are like IDs for elements. I removed the with statement after @whale_cancer said I should be calling it from obj_element. I changed that and it still doesn't work.Sorry, you posted above that it was called by with(obj_selection){scr_sleection()}, so I guess that confused me.
I guess I'll ask an obvious question: Is obj_selection flagged as visible?
Do your global variables hold true or false values? If so, I'd suggest using the keywords 'true' and 'false,' rather than 1 and 0, as it makes your code a little more readable (in my opinion).
That was actually an alternate attempt of making this kind of game using Drag and drop. But I eventually settled with a click and click approach, similar to Doodle God, Doodle Alchemy. Idk why I prefer left released though, I changed it to left pressed, still not working,Sorry for asking so many questions, but I'm trying to narrow down the problem, as it seems like it should work.
So, next question is why the mouse released and not mouse pressed? Are you dragging an element to a position and dropping it there?
Nope it didn't work. Probably because it's treating inst_x and inst_y as variables. But I didn't get the error message like inst_x or inst_y not defined. Yeah same for me it was night but now it's in the morning, glad you'd still like to help today.Hmm, I'm really kind of stumped now. the only thing I can think to suggest is to change instance_create(inst.x, inst.y, obj_selection); to instance_create(inst_x, inst_y, obj_selection);
All that's really doing though is using the variables that hold the same values as you have it now. Other than that, I'm really not sure.
It's getting late here, so that's it for me tonight. If I think of anything in the morning, I'll let you know. Sorry I couldn't be more help.
///Object oA create event
event_inherited();
str = "air"; //oE is the same, except str is "earth"
///oParent Create event
str = 0;
global.selectedA = 0;
global.selectedB = 0;
///oParent Mouse Enter event
global.hover = str;
show_debug_message(global.hover);
///oParent Mouse Leave Event
global.hover = 0;
show_debug_message(global.hover);
///oParent Mouse Left Click event
var inst = noone;
var inst2 = noone;
if (global.hover != 0)
{
if (global.selectedA == 0)
{
global.selectedA = global.hover;
}
else if (global.selectedB == 0)
{
global.selectedB = global.hover;
}
global.hover = 0;
if (global.selectedA != 0)
{
inst = instance_position(mouse_x, mouse_y, oParent);
if (inst != noone)
{
inst2 =instance_create(inst.x+40, inst.y, oSelect);
}
}
}
show_debug_message(string(inst));
show_debug_message(global.hover);
show_debug_message(global.selectedA);
show_debug_message(global.selectedB);
show_debug_message(string(inst2));