Alice Community

Alice Community (http://www.alice.org/community/index.php)
-   How do I...? (http://www.alice.org/community/forumdisplay.php?f=16)
-   -   Stop currently running method? (http://www.alice.org/community/showthread.php?t=11526)

cliftonb 07-20-2016 05:28 PM

Stop currently running method?
 
I'm using the helicopter class in an assignment and it has a built in method, "heli blade," that makes the rotors spin an infinite number of times. Is there a way to stop this method so the helicopter could, for instance, come into view, land, and turn off the blades?

I've made a little program where people are stranded on the roof of a burning building and a helicopter comes to rescue them...but turns around and drops them off on top of the building again! A little hokey, but good practice :)

chickentree 07-21-2016 12:35 PM

Bad Method, Bad Bad Method
 
[QUOTE=cliftonb;57225]I'm using the helicopter class in an assignment and it has a built in method, "heli blade," that makes the rotors spin an infinite number of times. Is there a way to stop this method so the helicopter could, for instance, come into view, land, and turn off the blades?

I've made a little program where people are stranded on the roof of a burning building and a helicopter comes to rescue them...but turns around and drops them off on top of the building again! A little hokey, but good practice :)[/QUOTE]

Hi Cliftonb,
The Heli blade method is a really good example of how not to do something.
1) In order to use it you must include it in a "Do together" block with your other instructions/methods because it is using infinite loops and so will never finish. You can see this by placing the following instructions into "My first method" in a new movie:
[CODE]
helicopter.heli blade
helicopter say Returned from blades
[/CODE][INDENT]When the movie is run, the only way to see the helicopter say "Return from blades" is to disable the helicopter.heli blades command.[/INDENT]2) Infinite loops in Alice tend to be resource hogs, meaning that they slow down the whole movie.
3) There is no way to control the blades. Once the method is called they are rotating, which prompted your question to start with. Once an infinite loop is started it will continue to run until the movie is stopped.

So what to do?
Take a lesson from helicopter.heli blade by writing you own method!
I would:
1) Insure that the helicopter is selected in the object list.[INDENT]Since the only thing affected by these changes will be the helicopter and its subobjects, the proper place for this type of method is within the object itself.[/INDENT]2) In the properties dialog click the Create new variable button.[INDENT]Give the variable a descriptive name (I used spinning)
Make it a Boolean variable (Booleans can have one of two values, true and false, which all we need.[/INDENT]3) Finally, set the initial value to false.
4) Click the Methods tab in the helicopter's dialog box and click the "create new method" button.[INDENT]Give the new method a descriptive name like spinBlades[/INDENT]5) Drag an If/Else statement into the spinBlades method.[INDENT]At the bottom of the condition popup select expressions and pick helicopter.spinning[/INDENT]6) Expand the helicopter in the objects tree so that you can see its subobjects.
7) Drag the propeller object into the spinBlades method you just created.[INDENT]Select the propeller.turn method
Set the direction to left (or right) and the amount to 1 revolution.
8) At the end of the turn instruction, click on "more...", select the duration and set it to 0.25.[/INDENT]9) Repeat step 7 and 8 only select the backRotor.

So now we have a helicopter whose blades will spin, if the helicopter.spinning variable is true, whenever the method helicopter.spinBlades is called.

Now for the magic...
1) In the Events editor click on the "create new event" button and choose "When the world starts"
2) Right click on the new "When the world starts" event and in the popup select change to and pick "While the world is running."[INDENT]Don't freak out, it's not as bad as it looks.[/INDENT]3) From the helicopter methods tab, drag your new spinBlades method into the "During" section of the "While the world is running" event.

This event will be called repeatedly whenever Alice has resources available; which pretty much means it will be called all the time in a normal movie. That is why we don't need any loops to keep the blades spinning. This makes understanding the movies code much easier!

And finally the test!
1) Select the "my first method" tab in the method editor.
2) Drag in a Loop and set the number of times to 5.
3) Drag a "do in order" block into the loop.
4) Add the following instructions to the "do in order" block.[INDENT]1. helicopter.spinning set value to not helicopter.spinning[INDENT]To do this, go to the properties tab of the helicopter and drag the spinning variable into your my first method's "do in order" block.
When the set value popup appears select expressions helicopter.spinning.
Click on the light blue arrow at the end of the set instruction.
In the popup select logic and "not helicopter.spinning."[INDENT]Since spinning is a Boolean, it can only hold one of two values, true or false. This statement just flips it from one to the other. If it is true at the beginning of the set instruction, it will be false at its completion and vice versa.[/INDENT][/INDENT]2. From the bottom of the method editor drag a wait statement into the "do in order" block and give it a value of 2.
3. Repeat steps one and two.
4. Save the movie
5. Click Play
[/INDENT]Assuming the value of helicopter.spinning starts off false, the code in "My first method" will cause the blades to spin for 2 seconds and then stop for 2 seconds. It will do this 5 times and then the movie will end.

One more item and some ideas:
If you like your new helicopter with the spinning blades, you can save it and use it in other movies, or even use it to create copies of your modified helicopter in the same movie. To do this:[INDENT]1. Right click on the helicopter in the objects window.
2. Rename it so that you and Alice don't get confused about which helicopter is which. Maybe myHelio.
3. Right click on your newly named myHelio and select "save object..."
4. Pay attention to where Alice is saving your new object.
[/INDENT]
There are lots of things you could do from here:
1. Change the style of the turn instruction from gently to abrupt and see which looks better.
2. Cause the rotors to spin at different rates.
3. Make myHelio methods to start and stop the blades by changing the value of myHelio.spinning. This is called encapsulation and keeps users of the object from having to know about it's internal workings.
4. Write a function isSpinning that returns true if the rotors are spinning.
5. Write new methods to cause the rotors to "spin up" to speed and "spin down" to a stop. Place these new methods in the Begin and End slots of the "While the world is running" event.
6. Fool around!

Hope this gives you some ideas
Mark

RavenOfCode 07-21-2016 03:08 PM

Try using a While loop, and have a bool parameter that you can turn it on and off with.

bodyjohn 11-19-2018 08:09 AM

[QUOTE=chickentree;57228]Hi Cliftonb,
The Heli blade method is a really good example of how not to do something.
1) In order to use it you must include it in a "Do together" block with your other instructions/methods because it is using infinite loops and so will never finish. You can see this by placing the following instructions into "My first method" in a new movie:
[CODE]
helicopter.heli blade
helicopter say Returned from blades
[/CODE][INDENT]When the movie is run, the only way to see the helicopter say "Return from blades" is to disable the helicopter.heli blades command.[/INDENT]2) Infinite loops in Alice tend to be resource hogs, meaning that they slow down the whole movie.
3) There is no way to control the blades. Once the method is called they are rotating, which prompted your question to start with. Once an infinite loop is started it will continue to run until the movie is stopped.

So what to do?
Take a lesson from helicopter.heli blade by writing you own method!
I would:
1) Insure that the helicopter is selected in the object list.[INDENT]Since the only thing affected by these changes will be the helicopter and its subobjects, the proper place for this type of method is within the object itself.[/INDENT]2) In the properties dialog click the Create new variable button.[INDENT]Give the variable a descriptive name (I used spinning)
Make it a Boolean variable (Booleans can have one of two values, true and false, which all we need.[/INDENT]3) Finally, set the initial value to false.
4) Click the Methods tab in the helicopter's dialog box and click the "create new method" button.[INDENT]Give the new method a descriptive name like spinBlades[/INDENT]5) Drag an If/Else statement into the spinBlades method.[INDENT]At the bottom of the condition popup select expressions and pick helicopter.spinning[/INDENT]6) Expand the helicopter in the objects tree so that you can see its subobjects.
7) Drag the propeller object into the spinBlades method you just created.[INDENT]Select the propeller.turn method
Set the direction to left (or right) and the amount to 1 revolution.
8) At the end of the turn instruction, click on "more...", select the duration and set it to 0.25.[/INDENT]9) Repeat step 7 and 8 only select the backRotor.

So now we have a helicopter whose blades will spin, if the helicopter.spinning variable is true, whenever the method helicopter.spinBlades is called.

Now for the magic...
1) In the Events editor click on the "create new event" button and choose "When the world starts"
2) Right click on the new "When the world starts" event and in the popup select change to and pick "While the world is running."[INDENT]Don't freak out, it's not as bad as it looks.[/INDENT]3) From the helicopter methods tab, drag your new spinBlades method into the "During" section of the "While the world is running" event.

This event will be called repeatedly whenever Alice has resources available; which pretty much means it will be called all the time in a normal movie. That is why we don't need any loops to keep the blades spinning. This makes understanding the movies code much easier!

And finally the test!
1) Select the "my first method" tab in the method editor.
2) Drag in a Loop and set the number of times to 5.
3) Drag a "do in order" block into the loop.
4) Add the following instructions to the "do in order" block.[INDENT]1. helicopter.spinning set value to not helicopter.spinning[INDENT]To do this, go to the properties tab of the helicopter and drag the spinning variable into your my first method's "do in order" block.
When the set value popup appears select expressions helicopter.spinning.
Click on the light blue arrow at the end of the set instruction.
In the popup select logic and "not helicopter.spinning."[INDENT]Since spinning is a Boolean, it can only hold one of two values, true or false. This statement just flips it from one to the other. If it is true at the beginning of the set instruction, it will be false at its completion and vice versa.[/INDENT][/INDENT]2. From the bottom of the method editor drag a wait statement into the "do in order" block and give it a value of 2.
3. Repeat steps one and two.
4. Save the movie
5. Click Play
[/INDENT]Assuming the value of helicopter.spinning starts off false, the code in "My first method" will cause the blades to spin for 2 seconds and then stop for 2 seconds. It will do this 5 times and then the movie will end.

One more item and some ideas:
If you like your new helicopter with the spinning blades, you can save it and use it in other movies, or even use it to create copies of your modified helicopter in the same movie. To do this:[INDENT]1. Right click on the helicopter in the objects window.
2. Rename it so that you and Alice don't get confused about which helicopter is which. Maybe myHelio.
3. Right click on your newly named myHelio and select "save object..."
4. Pay attention to where Alice is saving your new object.
[/INDENT]
There are lots of things you could do from here:
1. Change the style of the turn instruction from gently to abrupt and see which looks better.
2. Cause the rotors to spin at different rates.
3. Make myHelio methods to start and stop the blades by changing the value of myHelio.spinning. This is called encapsulation and keeps users of the object from having to know about it's internal workings.
4. Write a function isSpinning that returns true if the rotors are spinning.
5. Write new methods to cause the rotors to "spin up" to speed and "spin down" to a stop. Place these new methods in the Begin and End slots of the "While the world is running" event.
6. Fool around!

Hope this gives you some [URL="max-muscles.net"]ideas[/URL]
Mark[/QUOTE]

thats a great and detailed explanation!


All times are GMT -5. The time now is 02:08 AM.

Copyright ©2024, Carnegie Mellon University
Alice 2.x © 1999-2012, Alice 3.x © 2008-2012, Carnegie Mellon University. All rights reserved.