2013-07-24 119 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]) 
    )