GameMaker: Studio

GameMaker: Studio

Zobrazit statistiky:
Making object A change sprite from a click of object B
I have this puzzle, hopefully you guys can help me figure it out:

When the player hovers over object A or B while they are not selected, they change their sprites from a mouse enter event.

When the player clicks on object A or B, they become selected. Only one object may be selected at a time.

So if object A is selected and object B is clicked to become selected, how does object A change its sprite back to the unselected sprite?

I cannot use a step event to check the variable holding information for the selected object, as then it would ruin the use of the mouse enter event (the step event would constantly change the sprite back to the unselected sprite even when the mouse is hovering over a object)

Can you guys help me? Thanks.
< >
Zobrazeno 113 z 13 komentářů
From your clicker object

with(selectableobject){
if other.clickableID = id selectable = true
if !other.clickableID = id selectable = false
}

clickableID is the storeage variable on your clicker of what item "should" be selectable and should be the exact ID of the clickable object. variable selectable is the local seletableobject variable for if I'm currently selected.
Naposledy upravil Blind; 7. čvc. 2015 v 16.39
BBX původně napsal:
From your clicker object

with(selectableobject){
if other.clickableID = id selectable = true
if !other.clickableID = id selectable = false
}

clickableID is the storeage variable on your clicker of what item "should" be selectable. variable selectable is the local seletableobject variable for if I'm currently selected.

Thanks for the quick reply.

I don't have a mastery of GML, but I can kinda of tell what this is trying to do (correct me if I am wrong please):

With the object that is not selected

if the local variable of the other object that is not selected is true...

and

if the local variable of the other object that is not selected is false...
BBX původně napsal:
From your clicker object

with(selectableobject){
if other.clickableID = id selectable = true
if !other.clickableID = id selectable = false
}

clickableID is the storeage variable on your clicker of what item "should" be selectable and should be the exact ID of the clickable object. variable selectable is the local seletableobject variable for if I'm currently selected.

DO NOT USE "=" for comparison. Use "==". It's a big headache saver.

Furthermore, learn the practice of ALWAYS putting the condition of "if" statement into brackets:

if (condition) { statement; statement; ... statement; }

Why?
Because if you later add additional content to the curly brackets {...}, then it will work; if you do not use the curly brackets, the second statement will not be conditional (only if the "if" is true), which is a nightmare to troubleshoot.


Thanks for the reply, Alcator. But can you help me understand what this code is trying to say? (in lame man's terms)
The Jew původně napsal:
(in lame man's terms)
Ha, I like that one.

Use the same object and duplicate it in the room, then try something like this:
//Global global.clicked_id = -1; //Best to declare this outside of the object //Create Event selected = false; hover = false; //Step Event if (position_meeting(mouse_x, mouse_y, obj_clickable)){ hover = true; //If mouse is over object, hover is true if (mouse_check_button_pressed(mb_left)){ global.clicked_id = id; //If mouse left clicks object, id is store globally } } else { hover = false; } if (global.clicked_id == id){ //Does the globally stored id match this object's id? selected = true; } else { selected = false; }

(Sorry for any typos)
Naposledy upravil Thew; 8. čvc. 2015 v 10.01
It was just psuedo code

Also, I've never had to use == in Gamemaker
BBX původně napsal:
It was just psuedo code

Also, I've never had to use == in Gamemaker

Trust me, learn it / switch to it.

The PROBLEM is that "=" can mean "assignment" (evaluate the stuff on the right and assign it to the variable on the left), but it can also mean "comparison".

And one of the coolest programming technique is to shorten code:
instead of:

if (a equals b) then varEqual = true else varEqual = false;

you would write:

varEqual = (a equals b).

But if "=" means both, you would get:

varEqual = a = b

And suddenly, you have a problem. Does this mean:
take the value of b and assign it to variable a, then take the new value of a and assign it to varEqual?
or does it mean "assign the logical value of the comparison between a and b and assign it to varEqual?

imagine:
a = 5;
b = 6;

varEqual = a = b

in the first style, varEqual will become 6. In the second, it will become "false".

You really do not want this kind of ambiguity in your code!

varEqual = (a == b) will always mean exactly one thing: compare the two, and store the result in varEqual.

I wouldn't want such ambiguity either, however... what kinda junky line contains two comparisons?
BBX původně napsal:
I wouldn't want such ambiguity either, however... what kinda junky line contains two comparisons?

Not two comparisons -- two assignments. That's actually quite common in C programming, in which an assignment is automatically an expression that can be further used.
I grew up with QBasic, so there's always only one assignment per line. Indeed, the difference between = and == is if precision is to be counted (such as if 1.00001 is equal to 1 or not)
Thew původně napsal:
The Jew původně napsal:
(in lame man's terms)
Ha, I like that one.

Use the same object and duplicate it in the room, then try something like this:
//Global global.clicked_id = -1; //Best to declare this outside of the object //Create Event selected = false; hover = false; //Step Event if (position_meeting(mouse_x, mouse_y, obj_clickable)){ hover = true; //If mouse is over object, hover is true if (mouse_check_button_pressed(mb_left)){ global.clicked_id = id; //If mouse left clicks object, id is store globally } } else { hover = false; } if (global.clicked_id == id){ //Does the globally stored id match this object's id? selected = true; } else { selected = false; }

(Sorry for any typos)

Okay I see what you're trying to do. This is sort of what I had in mind, but I didn't know you could put events inside of events (having the mouse button press in the if statement). This is all cool knowing that it's inside the step event too, so I can remove my separate code dealing with mouse enter and exit assigning sprites.
Indeed, you can have every single action in the entire program run from a single "Main" object, with zero code on the objects themselves.
Naposledy upravil Blind; 8. čvc. 2015 v 17.11
It works! Thanks guys. I learn more about gamemaker every day...
< >
Zobrazeno 113 z 13 komentářů
Na stránku: 1530 50

Datum zveřejnění: 7. čvc. 2015 v 16.34
Počet příspěvků: 13