PDA

View Full Version : Identical click behavior for a group of objects


ohioboater
04-25-2014, 06:01 PM
Apologies if this is an obvious question. My son has a world with 6 buildings in it. For 5 of those buildings, if the user clicks on the building, the world should display a message that the building is locked. He knows how to do this with a series of nested if/else statements, but is there a more elegant way to do this? His idea was to have a list of buildings, and when an object is clicked, check to see if that object is a member of that list. But that doesn't seem to be the way lists/arrays work in Alice. But then I'm learning this along with him, so I could very well be missing something.

This is in Alice 2. We've been using the Duke tutorials and the Dann/Cooper/Pausch textbook.

sfunk
04-25-2014, 07:24 PM
A list is exactly what you want to use. make a list of the buildings that are locked. then use the for all function in an if statement and make sure that list is in the function.

ohioboater
04-29-2014, 07:44 PM
Cool, thank you for the reply.

MrMoke
04-29-2014, 10:26 PM
Your son is correct about the use of the lists.
One of the possible events is the "When mouse clicked on anything" do "something".

First, create the list.

Next, create a method that will check the list, and add an object parameter.

Next, create the event and replace "Nothing" with the name of the method. This will pass the "what was clicked" object as the parameter.

Now, pull in a "for all in order" into the method, and use an If to check the parameter object against each member, and says "locked" if a match is found.

Same code will work for any size list.