Programming morphs - page 8 - animation
Last updated at 9:20 pm UTC on 1 January 2019
Morphs can also become active, asking the system to inform them whenever a certain amount of time has passed. The mechanism is very simple, and its applications are broad.
First, morphs that wish to be active must implement a step method which is to be called periodically. Second, morphs may implement stepTime, which returns the number of milliseconds that should pass between calls to step.
The relevant code for FlasherMorph may be viewed to the left. Since stepTime is 500, a FlasherMorph's step method will be called twice per second. The step method of a FlasherMorph then changes the color that is being displayed.
Now you give it a try! Modify joe, which is now a FollowerMorph, so that every 10 milliseconds he adjusts his position to be 150 pixels to the left of jane. jane may be accessed from code in FollowerMorph under the name "morphToFollow".
flasher := FlasherMorph new openInWorld.
joe := FollowerMorph new openInWorld.
jane := Morph new openInWorld.
joe morphToFollow: jane.