2015-06-20 74 views
0

我正在編寫一個簡單的光學錯覺應用程序來學習如何在基維編碼。我想知道爲什麼我的程序崩潰以及如何解決它。 如果我取消註釋animation.start(self.c4)行然後我的程序運行良好,所以我幾乎可以確定問題是兩個動畫在同一時間運行。什麼是錯誤以及如何解決它?基維同時動畫

我.py文件:

from kivy.app import App 
from kivy.uix.widget import Widget 
from kivy.properties import NumericProperty, ObjectProperty,\ 
ReferenceListProperty 
from kivy.graphics import Color 
from kivy.core.window import Window 
from kivy.animation import Animation 

class Circle(Widget): 
    v = NumericProperty(0) 

class Illusion(Widget): 
    c1 = ObjectProperty(None) 
    c2 = ObjectProperty(None) 
    c3 = ObjectProperty(None) 
    c4 = ObjectProperty(None) 

    def __init__(self, **kwargs): 
     super(Illusion, self).__init__(**kwargs) 

     objects = (self.c1, self.c2, self.c3, self.c4) 
     values = (0,1,0,0) 

     for i, item in enumerate(objects): 
      objects[i].v = values[i] 

     self.start_illusion(instance = None, value = None) 

    def start_illusion(self, instance, value): 
     animation = Animation(v = 1, d = .25) 
     animation.bind(on_complete=self.continue_illusion) 
     animation.start(self.c3) 
     animation.start(self.c4) 

    def continue_illusion(self, instance, value): 
     animation = Animation(v = 0, d = .25) 
     animation.bind(on_complete=self.start_illusion) 
     animation.start(self.c3) 
     animation.start(self.c4) 

class IllusionsApp(App): 
    Window.clearcolor = (0,0,1,1) 
    def build(self): 
     return Illusion() 

if __name__ == '__main__': 
    IllusionsApp().run() 

我.kv文件:

<Circle>: 
    canvas: 
     Color: 
      hsv: 0,0,self.v 
     Ellipse: 
      pos: self.pos 
      size: self.size 

<Illusion>: 
    c1: _c1 
    c2: _c2 
    c3: _c3 
    c4: _c4 

    Circle: 
     id: _c1 
     size: 250,250 
     center_x: self.parent.width/4 
     center_y: self.parent.height/2 

    Circle: 
     id: _c2 
     size: 250,250 
     center_x: self.parent.width * 3/4 
     center_y: self.parent.height/2 

    Circle: 
     id: _c3 
     size: 180,180 
     center_x: self.parent.width/4 
     center_y: self.parent.height/2 

    Circle: 
     size: 180,180 
     id: _c4 
     center_x: self.parent.width * 3/4 
     center_y: self.parent.height/2 

回答

2

我認爲這個問題可能是用的on_complete功能每次調用重建新Animation

嘗試使用repeat animation property.

def __init__(self, **kwargs): 
     super(Illusion, self).__init__(**kwargs) 

     objects = (self.c1, self.c2, self.c3, self.c4) 
     values = (0,1,0,0) 

     for i, item in enumerate(objects): 
      objects[i].v = values[i] 

     self.animation_v0 = Animation(v = 0, d = .25) + Animation(v = 1, d = .25) 
     self.animation_v0.repeat = True 
     self.animation_v1 = Animation(v = 1, d = .25) + Animation(v = 0, d = .25) 
     self.animation_v1.repeat = True 

     #self.animation_v0.bind(on_complete=self.start_illusion) 
     #self.animation_v1.bind(on_complete=self.continue_illusion) 

     self.animation_v0.start(self.c3) 
     self.animation_v1.start(self.c4) 

    def start_illusion(self, instance, value): 
     self.animation_v0.start(self.c3) 

    def continue_illusion(self, instance, value): 
     self.animation_v1.start(self.c4)