混乱影响动画

2024-04-28 13:45:06 发布

您现在位置:Python中文网/ 问答频道 /正文

杂波没有做完整的动画。在

这是我当前的代码:

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

对于那些不喜欢礼物的人,我的问题是用文字描述的: 我要演员从中间移到右边,然后一直移到左边。相反,它只是从中间直接向左移动。在

是什么造成的,我该怎么解决?在


Tags: toimporteventmaindefsysstageactor
3条回答

当你对一行接一行的时候

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

克拉特不用等另一个完成就可以完成这两个任务。这意味着在第二个命令接管之前,第一个命令几乎没有时间移动代理。在

以下是使用“已完成”信号的示例:

^{pr2}$

Here is the documentation on clutter animations
Here is the documentation on the "completed" signal
Here is some working example code

尝试以下代码:

def onClick(actor, event):
    animation1 = actor.animatev(Clutter.AnimationMode.LINEAR, 1000, ["x"], [280])
    animation1.connect_after(
        'completed',
        lambda animation: actor.animatev(Clutter.AnimationMode.LINEAR, 1000, ["x"], [20])
    )

就像文档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])

完全等同于:

^{pr2}$

这就是你所看到的。在

如果要链接两个动画,则必须使用connect_after函数连接到ClutterAnimationcompleted信号,以便杂波可以创建新的动画:

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()和{}是不推荐使用的;可以使用显式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的状态时都创建隐式动画,但我将把这作为练习留给读者。在

相关问题 更多 >