[Newbies] Animation question

Bert Freudenberg bert at freudenbergs.de
Thu Oct 12 17:25:11 UTC 2017


On Thu, Oct 12, 2017 at 6:24 PM, obrienj <obrienj at protonmail.com> wrote:

> I am having difficulty getting the following example to work using either
> delay or step:
>
> aMorph := Morph new.
> aMorph position: (100 at 100).
> aMorph openInWorld.
> 30 timesRepeat: [aMorph position: aMorph position +(1 at 0). Transcript
> show: 'test '; cr.].
> (aMorph position) >= (120 at 100) ifTrue: [aMorph color: Color yellow].
> Transcript show: 'position: ', aMorph position; cr.
>
> For example:
>
> aMorph := Morph new.
> aMorph position: (100 at 100).
> aMorph openInWorld.
> delay := Delay forMilliseconds: 20.
> [30 timesRepeat: [aMorph position: aMorph position +(1 at 0). delay wait.].]
> fork.
> (aMorph position) >= (110 at 100) ifTrue: [aMorph color: Color yellow].
> Transcript show: 'position: ', aMorph position; cr.
>
> In the first example the morph moves, changes color, and the Transcript
> shows its final position. However, in the second example the morph moves
> but does not change color and the Transcript shows its starting position.
>


Morphic animation is based on stepping. That means, you don't move a Morph
repeatedly in a loop, but Morphic tells your morph to update its position
repeatedly.

You need to make your own Morph subclass and implement a "step" method.
Whenever that method is called, it moves the morph by a little. For
example, you could use an existing method called" translateBy:"

| aMorph |
aMorph := Morph new openInWorld.
aMorph startStepping: #transformedBy: at: Time millisecondClockValue
arguments: {MorphicTransform offset: -3 @ -3} stepTime: 200.

However, it's unlikely no existing method does your specific animation, so
you indeed need to implement it, eg

step
    self position: self position + (1 at 0).
    (self position) >= (120 at 100) ifTrue: [self color: Color yellow].

So this is the Right Way. Without bad hacks you cannot do meaningful
animation from a workspace. There is a hack however: If you insert "World
doOneCycle" in the while loop of your first example, it will actually work,
because it will redraw the world in each loop. However, this would prevent
two objects animating independently, that's why the design calls for the
"step" method approach.

- Bert -
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.squeakfoundation.org/pipermail/beginners/attachments/20171012/d7ac2204/attachment.html>


More information about the Beginners mailing list