【发布时间】:2019-04-14 17:44:00
【问题描述】:
我有几个问题:
我有旋转球的动画,它应该始终在屏幕顶部,并且屏幕应该始终只显示它的一半。我可以做到这一点,但只能通过单击按钮调用将球带到正确位置的函数。我需要球总是在正确的位置,不仅然后我点击按钮。我尝试使用 init 函数,但它总是给我这个错误:'super' object has no attribute 'getattr'。我也尝试使用 getattr 而不是 init,但它没有将球带到正确的位置。
所以我想单击一个按钮,然后球开始旋转几秒钟。当它停止时,我希望球是另一种颜色,甚至是球的另一种图像。我尝试使用 on_complete 但我不明白应该在哪里使用它。
我的主py文件:
from kivy.app import App
from kivy.uix.screenmanager import Screen
from kivy.lang import Builder
from kivy.animation import Animation
from kivy.properties import NumericProperty
class OpenScreen(Screen):
angle = NumericProperty(0)
#Trying to make __init__ function
#def __init__(self, **kwargs):
#super(OpenScreen, self).__init__(**kwargs)
#y = self.height
#y1 = self.width / 2
#self.ids.test.pos = 0, y - y1
#Function of taking ball in the right place and spinning it
def z(self):
anim = Animation(angle=360, duration=0.5)
anim += Animation(angle=360, duration=0.5)
anim.start(self)
y = self.height
y1 = self.width / 2
self.ids.test.pos = 0, y - y1
self.ids.test.source = 'redball.png'
def on_angle(self, item, angle):
if angle == 360:
item.angle = 0
...screens classes...
GUI = Builder.load_file('game.kv')
class GameApp(App):
def build(self):
return GUI
def change_screen(self, screen_name):
screen_manager = self.root.ids['screen_manager']
screen_manager.current = screen_name
GameApp().run()
我的 Openscreen.kv 文件:
#:kivy 1.10.1
<OpenScreen>:
FloatLayout:
canvas:
Rectangle:
size: self.size
pos: self.pos
source: 'bg.png'
Image:
id: test
size_hint_y: None
height: self.width
source: 'blueball.png'
allow_stretch: True
keep_ratio: False
canvas.before:
PushMatrix
Rotate:
angle: root.angle
axis: 0, 0, 1
origin: self.center
canvas.after:
PopMatrix
Button:
text: "spin"
font_size: self.height - 29
valign: 'middle'
halign: 'center'
padding: 2,2
size_hint: .7, .1
pos_hint:{"x":.15, "y":.585}
background_color: 0,0,0,0
on_press:
root.z()
....
我该怎么做?
【问题讨论】:
-
尝试在您的
__init__()中使用Clock.schedule_once()安排呼叫设置球。