python - 困惑搞乱动画

标签 python linux animation clutter

Clutter 没有完成完整的动画。

这是我当前的代码:

from gi.repository import Clutter, Gtk
import sys

def onClick(actor, event):
    actor.animatev(Clutter.AnimationMode.LINEAR, 1000, ["x"], [280])  # clutter does not seem to be running this line
    actor.animatev(Clutter.AnimationMode.LINEAR, 1000, ["x"], [20])

def main():
    Clutter.init(sys.argv)

    # Colors
    red = Clutter.Color().new(255, 0, 0, 255)
    black = Clutter.Color().new(0, 0, 0, 255)

    # Create Stage
    stage = Clutter.Stage()
    stage.set_title("Basic Usage")
    stage.set_size(400, 200)
    stage.set_color(black)

    # Rectangle Actor
    actor = Clutter.Rectangle()
    actor.set_size(100, 50)
    actor.set_position(150, 100)
    actor.set_color(red)
    actor.set_reactive(True)
    actor.connect("button-press-event", onClick)

    # Add Actor to the Stage
    stage.add_actor(actor)
    stage.connect("destroy", lambda w:  Clutter.main_quit())
    stage.show_all()

    Clutter.main()

if __name__ == '__main__':
    main()

看看我的问题的这个例子:

enter image description here

对于那些不喜欢 gif 的人,这是我用文字描述的问题: 我想让 Actor 从中间向右移动,然后一直向左移动。相反,它只是从中间直接向左移动。

是什么原因造成的,我该如何解决?

最佳答案

就像 ClutterActor.animate() 的文档说的那样:

Calling this function on an actor that is already being animated will cause the current animation to change with the new final values, the new easing mode and the new duration https://developer.gnome.org/clutter/stable/clutter-Implicit-Animations.html#clutter-actor-animate

这意味着下面的代码:

actor.animatev(Clutter.AnimationMode.LINEAR, 1000, ["x"], [280])
actor.animatev(Clutter.AnimationMode.LINEAR, 1000, ["x"], [20])

完全等同于:

actor.animatev(Clutter.AnimationMode.LINEAR, 1000, ["x"], [20])

这就是您所看到的。

如果你想链接两个动画,你必须连接到 ClutterAnimationcompleted 信号,使用 connect_after 函数,这样杂乱可以创建一个新的动画:

def moveLeft (animation, actor):
    actor.animatev(Clutter.AnimationMode.LINEAR, 1000, ["x"], [20])

actor.animatev(Clutter.AnimationMode.LINEAR, 1000, ["x"], [280]).connect_after('completed', moveLeft)

我想指出 animatev()ClutterAnimation 已弃用;它们可以通过使用显式 Clutter.KeyframeTransition 或隐式转换来替换,例如:

from gi.repository import Clutter

Clutter.init(None)

stage = Clutter.Stage()
stage.connect('destroy', lambda x: Clutter.main_quit())

actor = Clutter.Actor()
actor.set_background_color(Clutter.Color.get_static(Clutter.StaticColor.RED))
actor.set_reactive(True)
actor.set_size(32, 32)
stage.add_child(actor)
actor.set_position(82, 82)

def moveLeft(actor):
    actor.set_x(20)

def moveRight(actor):

    actor.set_easing_duration(1000)
    actor.set_easing_mode(Clutter.AnimationMode.LINEAR)
    actor.set_x(280)
    actor.connect('transition-stopped::x', lambda a, n, t: moveLeft(actor))

actor.connect('button-press-event', lambda a, e: moveRight(actor))

stage.show()
Clutter.main()

它可以比这更复杂;您还需要记住断开 transition-stopped::x 信号处理程序,并恢复缓动状态以避免每次更改 actor 的状态时都创建隐式动画,但我将其保留为给读者练习。

关于python - 困惑搞乱动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17824988/

相关文章:

linux - 文字索引

c++ - 如何获得连接路由中的跃点?

WPF - 颜色动画完成事件

ios - UICollectionView 布局更改的默认动画持续时间是多少?

python - 无法在 python 中使用 setsockopt 进行连接

python - cx_Oracle - DLL 加载失败

python - opencv python使用roxPolyDP在植物托盘中找到正方形

python - 切片 numpy 数组以获得第 n 列

linux - 如何从 .sh 文件通过串行向 Arduino 发送命令?

javascript - 循环后暂停 css sprite 一秒钟