PDA

View Full Version : More on mouse events


jrrTolkien
09-22-2014, 07:14 AM
Is there a way to store the values of the objects the mouse selects in a variable? I usually have to make an event for when the mouse clicks on each single object, which gets kinda sloppy. If I said when the mouse clicks on anything, and stored what the mouse clicked in a variable, I could use if/else statements to trigger the right method, making it much easier to code when there are a lot of objects I want the user to click.

-jrr

MrMoke
09-22-2014, 07:49 AM
Add an Object Parameter to the Method you wish to call. Drag the method name into the event, and there will be a dropdown for "what was clicked".

It obviously gets more complicated if you have different types of objects because the parts are name differently.

jrrTolkien
09-22-2014, 07:59 AM
Great! That'll make things a lot easier (I hope :p) Thanks!

-jrr

MrMoke
09-22-2014, 08:05 AM
Forgot the Demo

jrrTolkien
09-22-2014, 09:18 AM
Happens to me all the time in emails! I keep forgetting the attachments! :o

AliceMaster00
09-23-2014, 03:09 PM
So, I took a look at the demo and found that even if you don't click on the t-rex, the action is still performed (its leg moves anyways). I messed around with the world and fixed the issue. There needs to be a method that checks what object was clicked, but it can't grab the variable that is in the parameter in a different method. So, all you do is link the object within the parameter to a world variable, preferably a string, so that the method that initiates the action upon the object that was clicked knows what action to initiate. You can skim through the coding to see what I did. Let me know if it was helpful. There are two dinos. If you click the trex, then the trex nods, if you click the triceratops, then the triceratops nods it's head, but, if you click on the ground or the sky, or any object that is not the trex or the triceratops, then nothing happens.

MrMoke
09-23-2014, 07:40 PM
A major portion of successful design is to create efficient and creative ways to pass information between modules. Global variables are a good way to make information accessible.
My original demo was just a quickie to show how to pass one of the same type of clicked objects into a method.

This one took a few more minutes, but accomplishes the same using lists of similar objects.

There's always a way, but you have to plan it first ;)

AliceMaster00
09-25-2014, 06:40 AM
Nicely done! Ha ha, I never thought of using lists. That's probably why I'm having so much trouble with my weapon swapping system.

RavenOfCode
09-25-2014, 07:43 PM
I saw your comment and I realized that's a smart idea... a weapon system with lists... :)