Quote:
Originally Posted by george3356
ASSIGNMENT: Make a rabbit chase a butterfly until it catches it w/ a net.
I am now trying to make sure my butterfly does not go to high or to low - so high the rabbit can't reach/crashing into the ground. I know I need an if statement here, but I'm just not sure what to do with it. Any thoughts?
(Again, I need to be sure the butterfly does not go to high or low using an IF/ELSE statement.)
|
Do not use methods with infinite loops!
Why? Well, because it can cause problems that are hard to find and because Alice tends to perform poorly when methods contain infinite loops, but more importantly there is a better alternative! The better alternative is an event and it replaces an infinite loop as follows:
- Write the method that you want to run continuously without an infinite loop.
- At the top of the Events section click on “create new event” and choose the “When the world starts” event.
- Right click on the left side of the new event where the dotted pattern is, highlight the “change to” option and click on “While the world is running.”
- Finally, left click on the pattern on the left side of your method in the “methods” section and drag the method into the “During”
Now when the movie starts so will your method and it will continue to be restarted whenever its end is reached. The running of other methods will not be affected and everyone is happy.
A few comments on events:
The event used above has three sections Begin, During and End (Programming Alice refers to it as a BDE event.) You can use one all or none of these “slots” in your event. Obviously using none of the options is useless but the others can come in handy if there are things you want to do before your During method starts or afterward in order to “clean up” when the during method ends. For the “When the world starts” event we are discussing here the “Begin” method would be called before the user interacts with the movie and the “End” event would execute as the movie was shutting down. As I stated above if you don't have any set up or clean up things that need to be done, just leave the slots blank.
There are other events that can be used to allow a method to be run only when certain conditions are met or when a variable changes. These events can be used to do things like keep an object from going too high or too low or almost any other condition you can think of and code.
Code:
While dragon is within(subject=dragon's height / 2) of ground is true
Begin: Nothing
During: dragon.moveUp
End: Nothing
In these situations it is better to use a general condition rather than something specific. For instance, if instead of using “dragon is within(subject=dragon's height / 2) of ground” I were to use “dragon distance above ground more... == (subject = dragon's height / 2)” the event would only be called if, when the event next checked, the dragon was EXACTLY “dragon's height/2” above the ground. If, when the condition was evaluated, the dragon was higher or lower than ½ its height above the ground, no action would be taken.
Another way to use these events is for things like making the helicopter blades spin when you want them. Using a Boolean variable as a flag to determine when to let the blades turn.
Code:
While helicopter.spinBlades is true
Begin: helicopter.spinUp
During: helicopter.spinning
End: helicopter.spinDown
In this example all three of the method “slots” are used. When helicopter.spinBlades ( which is a Boolean variable I created) becomes true, the Begin method (helicopter.spinUp) is called causing the blades to spin slowly at first and then faster and faster. When the helicopter.spinUp method completes, the event automagicly calls the “During” method (helicopter.spinning) and continues to call this method as long as the movie is running and helicopter.spinBlades is true. If at any time helicopter.spinBlades changes to false, maybe because the helicopter has landed or its engine has failed, then the “End” method is called, in this case causing the blades to slow down and stop.
One last point, if an infinite loop is used in a method the only way to call other methods is to contain them in a “Do together” block with the infinite loop. By default methods act sequentially, one after another, so Alice calls a method and then waits for it to end before going on to the next or instruction. In the case of a method with an infinite loop the method will never end because the loop will never end and so any methods or instructions following that method will not be executed. The only instructions that could be called are ones that are either in a “Do Together” block with our infinite instruction or ones that are in events become true or “fire.”
Code:
world.my first method
Do together
print This print will execute once.
While true
print While true is always true and will never stop.
Wait 5 seconds
// End of Do together
print This print will never execute.