Python 乱七八糟的动画
杂波没有完成完整的动画 这是我当前的代码:Python 乱七八糟的动画,python,linux,animation,clutter,Python,Linux,Animation,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.Anima
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()
看这幅我的问题的插图:
对于那些不喜欢GIF的人,以下是我用文字描述的问题:
我希望演员从中间向右移动,然后一直向左移动。相反,它只是从中间一直向左移动
是什么导致了这种情况,我如何修复它?当您对紧随其后的行执行这些操作时
def onClick(actor, event):
actor.animatev(Clutter.AnimationMode.LINEAR, 1000, ["x"], [280])
actor.animatev(Clutter.AnimationMode.LINEAR, 1000, ["x"], [20])
杂乱无章地完成了这两个任务,而不必等待另一个任务完成。这意味着在第二个命令接管之前,第一个命令几乎没有时间移动代理
以下是使用“完成”信号的示例:
尝试以下代码:
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])
)
actor.animatev(Clutter.AnimationMode.LINEAR, 1000, ["x"], [280])
actor.animatev(Clutter.AnimationMode.LINEAR, 1000, ["x"], [20])
如ClutterActor.animate()的文档所示: 对已设置动画的参与者调用此函数将 使当前动画随新的最终值更改 新的宽松模式和新的持续时间 这意味着以下代码:
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])
)
actor.animatev(Clutter.AnimationMode.LINEAR, 1000, ["x"], [280])
actor.animatev(Clutter.AnimationMode.LINEAR, 1000, ["x"], [20])
完全等同于:
actor.animatev(Clutter.AnimationMode.LINEAR, 1000, ["x"], [20])
这就是你看到的
如果要链接两个动画,必须使用connect\u after
功能连接到ClutterAnimation
的completed
信号,以便Clutter可以创建新动画:
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
是不推荐的;可以使用显式杂波。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
信号处理程序的连接,并恢复放松状态,以避免每次更改演员的状态时都创建隐式动画,但我将把它作为练习留给读者。您介意切换动画语句的顺序并告诉我它的作用吗?“我想我知道这个问题的答案。”斯蒂芬,谢谢。切换两行使它从中间向右移动,而不是从中间向左移动。好的,我知道答案,马上发布。。。见鬼,我们应该这样互相解释我们的问题+1只为了gif!当我在两行之间添加这个时:while(actor.get_x()<280):继续我的CPU峰值,它永远不会做任何事情。“演员待在原地不动。”丹尼尔塔我也很怀疑,你需要用合法的方式,而不是用黑客的方式。我完成了真正的答案recently@DanielTA很抱歉回答不好,在我提交这篇文章时,这只是一个临时的破解,请查看更新的答案。你越来越近,它会将它移到左边,但马上,它就没有动画了。它在右边动画,然后就出现在左边。@DanielTA是我想要模仿的,也许读一下,然后试着调整一下?我会继续调查的