GameMaker: Studio

GameMaker: Studio

データを表示:
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.
< >
1-13 / 13 のコメントを表示
Blind 2015年7月7日 16時38分 
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.
最近の変更はBlindが行いました; 2015年7月7日 16時39分
BBX の投稿を引用:
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 の投稿を引用:
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 の投稿を引用:
(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)
最近の変更はThewが行いました; 2015年7月8日 10時01分
It was just psuedo code

Also, I've never had to use == in Gamemaker
BBX の投稿を引用:
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.

Blind 2015年7月8日 11時55分 
I wouldn't want such ambiguity either, however... what kinda junky line contains two comparisons?
BBX の投稿を引用:
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.
Blind 2015年7月8日 14時14分 
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 の投稿を引用:
The Jew の投稿を引用:
(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.
Blind 2015年7月8日 17時11分 
Indeed, you can have every single action in the entire program run from a single "Main" object, with zero code on the objects themselves.
最近の変更はBlindが行いました; 2015年7月8日 17時11分
It works! Thanks guys. I learn more about gamemaker every day...
< >
1-13 / 13 のコメントを表示
ページ毎: 1530 50

投稿日: 2015年7月7日 16時34分
投稿数: 13