2013-07-24 100 views
3

杂乱没有做完整的动画。杂乱搞乱动画

这是我当前的代码:

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,这里所描述我的问题单词: 我希望演员从中间移动到右边,然后一直移动到左边。相反,它只是从中间直线向左移动。

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

+1

不错的gif,你真的解释了你的问题。 – Stephan

+0

你介意改变动画语句的顺序并告诉我它的功能吗?我想我知道这个答案。 – Stephan

+0

@Stephan谢谢。切换两条线使其从中间向右移动,而不是从中间移动到左边。 – DanielTA

回答

2

像ClutterActor的文档.animate()说:

调用对已被动画将 导致当前动画与新的最终值发生变化, 新的宽松模式和新的持续时间 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信号处理程序,并恢复缓动状态以避免每次更改演员状态时都创建隐式动画,但我会将其作为练习留给读者。

+0

感谢您指出animatev已折旧。 – DanielTA

0

当对方

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

杂波后立即做这些对线做他们既无需等待其他完成。这意味着在第二个命令接管之前,第一个命令几乎没有时间移动代理。

下面是使用 “已完成” 信号的一个例子:

def onClick(actor, event): 
    animate(actor) 

def animate(actor): 
    firstAnimation = actor.animatev(Clutter.AnimationMode.LINEAR, 1000, ["x"], [280]) 

    firstAnimation.connect_after("completed", moveLeft) 

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

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

+0

当我在两条线之间加上这个时: while(actor.get_x()<280): 继续 我的CPU高峰期,它从来没有做任何事情。演员保持放置。 – DanielTA

+0

@DanielTA我很怀疑,你需要以合法的方式来做,而不是黑客的方式。我最近完成了真正的答案 – Stephan

+0

@DanielTA对不起,这只是一个临时黑客,而我提交了这个,请看到更新的答案。 – Stephan

1

尝试以下代码:

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]) 
    )