Forum Archive

Layer animate 'rotate' question

wradcliffe

I am working with the layer.animate('rotate' scene function and it works as expected. I do not understand how to rotate something back and forth - say +90 and then -90.

This combination does not work - only the second call seems to execute:

self.layer.animate('rotation', self.layer.rotation + 90, duration=0.25, autoreverse=True)
self.layer.animate('rotation', self.layer.rotation - 90, duration=0.25, autoreverse=True)

and so I tried this which also does not work:

self.layer.animate('rotation', self.layer.rotation + 90, duration=0.25, autoreverse=True)
self.layer.animate('rotation', self.layer.rotation - 90, duration=0.25, delay=0.25, autoreverse=True)

does this mean I have to execute the second animate via a completion function, or is there a way to hack a curve that would cause it to mirror + to -?

JonB

Unlike ui animations, I think scene only lets you have an active animation one at a time for a given attribute.
The key to chaining animations is to use the completion argument:


from scene import *
import sys
class MyScene (Scene):
    def setup(self):
        self.layer = Layer(Rect(self.size.w * 0.5 - 100,
                                self.size.h * 0.5 - 100, 200, 200))
        self.layer.background = Color(1, 0, 0)
        def clockwise(completion=None):
            self.layer.animate('rotation', self.layer.rotation+90, duration=1.0,
                           autoreverse=True,completion=completion)
        def counterclockwise(completion=None):
            self.layer.animate('rotation', self.layer.rotation-90, duration=1.0,
                autoreverse=True,completion=completion)
        clockwise(counterclockwise)
    def draw(self):
        background(0, 0, 0)
        self.layer.update(self.dt)
        self.layer.draw()
run(MyScene())