10

I have a Square, and I want to move and scale it at the same time:

sq = Square()
self.add(sq)
self.play(ApplyMethod(sq.scale, 0.5), ApplyMethod(sq.move_to, UP*3))

However, the first animation is skipped and only the last one works.

Is there a simpler solution than using Transform?

Chris
  • 1,206
  • 2
  • 15
  • 35
James Hao
  • 765
  • 2
  • 11
  • 40

1 Answers1

14

There are 3 ways:

class MultipleMethods1(Scene):
    def construct(self):
        sq = Square()
        self.add(sq)
        self.play(
            sq.scale, 0.5,
            sq.move_to, UP*3,
            run_time=5
        )
        self.wait()

class MultipleMethods2(Scene):
    def construct(self):
        sq = Square()
        cr = Circle()
        VGroup(sq,cr).arrange(RIGHT)
        self.add(sq)
        def apply_function(mob):
            mob.scale(0.5)
            mob.shift(UP*3)
            return mob

        self.play(
            ApplyFunction(apply_function,sq),
            ApplyFunction(apply_function,cr),
            run_time=5
        )
        self.wait()

class MultipleMethods3(Scene):
    def construct(self):
        sq = Square()
        self.add(sq)
        sq.generate_target()
        sq.target.scale(0.5)
        sq.target.move_to(UP*3)

        self.play(
            MoveToTarget(sq),
            run_time=5
        )
        self.wait()
TheoremOfBeethoven
  • 1,864
  • 6
  • 15
  • Thanks very much, that saved my life, one question: what's the difference between the method1 and ApplyMethod of my post? – James Hao Aug 05 '19 at 03:08
  • We could say that ApplyMethod is already discontinued, but it is useful for new learners to understand. In general, you can apply this format to animate the change of a property of a Mobject, a more advanced example is [this](https://github.com/Elteoremadebeethoven/AnimationsWithManim/blob/master/English/extra/faqs/faqs.md#arrange-objects). – TheoremOfBeethoven Aug 05 '19 at 19:23
  • 3
    The `MultipleMethods1` above can no longer be used (since passing Mobject methods to Scene.play is no longer supported). – xax Dec 10 '22 at 06:42
  • Thank you for the informative answer! I kindly ask how could I have a dynamic approach of the second method. For example, can I pass `0.5` and `3` as parameters? – M. Al Jumaily Mar 30 '23 at 00:03