View Full Version : Help understanding loop control in Alice 2

05-22-2014, 09:13 AM
I'm back with what is probably another very basic question.

My son built a variation on a whack-a-mole game. His main method moves an invisible squirrel a random X-Y distance from an invisible reference object, turns the squirrel visible for a short period, then changes it back to invisible and moves it back to the reference object. This method is enclosed in an infinite loop. If the user clicks on the squirrel, a method is called to have the squirrel change its height (a simple version of being squished) and say "Ow!"

The game works after a fashion, but we can't figure out how to get the loop to pause while the "squish" method is happening. I suggested that he replace the infinite loop with a while loop that checks a Boolean variable called Squished. While Squished is false, the main loop runs. When the user successfully clicks the squirrel and calls the squish method, the first thing the method does is change that value to True, and the last is to change it back to False.

With my basic understanding of how Alice works, it seems that this approach should make the squirrel stay in place and remain visible until the squish method finishes running, at which point the main method will start running again. But that's not what happens when we run the world. The main method seems to just keep plugging along, so that the squirrel's comment happens when it is invisible and moving back to center.

Not sure if this is because we aren't understanding the way Alice prioritizes events, loops, etc., or if he just wrote his main method wrong.

I'm as much interested in understanding how events and methods interact and get prioritized in Alice as I am in a specific solution to this particular world's bug. Any help would be much appreciated.

05-22-2014, 09:42 AM
So I figured out this out myself. Instead of just deleting the question, I'll summarize the answer in case it helps someone else.

His world had 2 problems:

1. He needed to enclose the conditional inside an infinite loop. Apparently a "while" statement will not start back up once its condition goes to false, so an infinite loop is necessary to get the while to notice that its condition is true again.

2. He was using three sets of instructions to move his squirrel - one set for moving forward/right and one set for backwards/left and then another for backward/right. He did that because he hasn't quite wrapped his head around the idea of using a negative to positive range to get motion in all 4 X/Y quadrants with a single random move command. Reducing that to a single set of instructions fixed the issue where the squish method happens while the squirrel is invisible and moving.