View Single Post
Bad Method, Bad Bad Method
Super Moderator
Status: Offline
Posts: 250
Join Date: Dec 2012
Location: Frosno, Ca
Default Bad Method, Bad Bad Method - 07-21-2016, 01:35 PM

Originally Posted by cliftonb View Post
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
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:
helicopter.heli blade
helicopter say Returned from blades
When the movie is run, the only way to see the helicopter say "Return from blades" is to disable the helicopter.heli blades command.
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.
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.
2) In the properties dialog click the Create new variable button.
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.
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.
Give the new method a descriptive name like spinBlades
5) Drag an If/Else statement into the spinBlades method.
At the bottom of the condition popup select expressions and pick helicopter.spinning
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.
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.
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."
Don't freak out, it's not as bad as it looks.
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.
1. helicopter.spinning set value to not helicopter.spinning
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."
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.
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
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:
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.
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 Henwood
Reply With Quote