我正在设置动画来调用另一个函数on_complete,然后使用重新定义的变量重复自己。除了我的变量没有改变所以它在生成的第一个小部件上应用重复的动画。这是一个例子:
from kivy.config import Config
Config.set('input', 'mouse', 'mouse,multitouch_on_demand')
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.uix.image import Image
from kivy.animation import Animation
from kivy.graphics import Color, Rectangle
from kivy.uix.button import Button
global Draw_Card
Draw_Card=0
global SpadeCount
SpadeCount=0
##Window.fullscreen = True
##Window.size = (1920, 1080)
Player1Deck=['Spade', 'Spade', 'Spade']
class Spade1(Widget):
def __init__(self, **kwargs):
super(Spade1, self).__init__(**kwargs)
global CardSetup
CardSetup=True
self.b=Button(text='Hello World!')
self.add_widget(self.b)
self.bind(pos=self.redraw, size=self.redraw)
self.size=(186.5 / 2, 259.25 / 2)
self.pos=(200, 50)
def Set_Originals(self, x):
self.Original_pos=x.pos
self.Original_size=x.size
def redraw(self, *args):
self.b.pos=self.pos
self.b.size=self.size
class Spade2(Widget):
def __init__(self, **kwargs):
super(Spade2, self).__init__(**kwargs)
global CardSetup
CardSetup=True
self.b=Button(text='Hello World!')
self.add_widget(self.b)
self.bind(pos=self.redraw, size=self.redraw)
self.size=(186.5 / 2, 259.25 / 2)
self.pos=(200, 50)
def Set_Originals(self, x):
self.Original_pos=x.pos
self.Original_size=x.size
def redraw(self, *args):
self.b.pos=self.pos
self.b.size=self.size
class Spade3(Widget):
def __init__(self, **kwargs):
super(Spade3, self).__init__(**kwargs)
global CardSetup
CardSetup=True
self.b=Button(text='Hello World!')
self.add_widget(self.b)
self.bind(pos=self.redraw, size=self.redraw)
self.size=(186.5 / 2, 259.25 / 2)
self.pos=(200, 50)
def Set_Originals(self, x):
self.Original_pos=x.pos
self.Original_size=x.size
def redraw(self, *args):
self.b.pos=self.pos
self.b.size=self.size
class DrawCard(Widget):
def __init__(self, **kwargs):
super(DrawCard, self).__init__(**kwargs)
self.anim_queue = []
self.Draw_Card()
def Draw_Card(self):
global Draw_Card
global CardSetup
global SpadeCount
if Draw_Card > 0:
NextCard=Player1Deck.pop(0)
if NextCard == 'Spade':
if SpadeCount==0:
self.add_widget(Spade1())
elif SpadeCount==1:
self.add_widget(Spade2())
elif SpadeCount==2:
self.add_widget(Spade3())
SpadeCount += 1
PlayerHandCardsList=self.children[:]
print(PlayerHandCardsList)
x=len(PlayerHandCardsList)-1
print(x)
print(PlayerHandCardsList[x])
anim = Animation(pos=((493.75) + ((x * 25)), 0),
t='in_out_quad', duration=1)
anim &= Animation(size=(186.5, 259.25),
t='in_out_quad', duration=1)
self.anim_queue.append((PlayerHandCardsList[x], anim))
w, a = self.anim_queue.pop(0)
a.bind(on_complete=lambda pos, size: self.WrapUpAnimation(PlayerHandCardsList, x))
a.start(w)
else:
CardSetup=False
def WrapUpAnimation(self, instance, x):
global Draw_Card
instance[x].Set_Originals(instance[x])
Draw_Card += -1
self.Draw_Card()
class Game(Widget):
def __init__(self):
super(Game, self).__init__()
global Draw_Card
Draw_Card += 3
self.add_widget(DrawCard())
class GameApp(App):
def build(self):
top = Widget()
top.add_widget(Game())
return top
if __name__ == '__main__':
GameApp().run()
当控制台打印出PlayerHandCardsList时,它会正确显示到目前为止添加的所有小部件。当它打印x时,这也是正确的。但是,当PlayerHandCardsList [x]打印时,它会显示分配的第一个值,并且在重复Draw_Card函数时不会更新。然而它确实正确地重新定义了位置x * 25.为什么这样做呢?请帮忙
答案 0 :(得分:0)
好的,所以我明白了!因为我每次都重新定义self.children列表而不是.appending,最新的项目总是在索引0!因此,为此,您需要做的就是为每次运行引用列表的索引0!