所以作为游戏的一部分,我有一个动画文本窗口,可以弹出每秒打印每个单词。为什么我只能在我的方法中只运行一行?
这里的问题是方法“eachwordprint”,在游戏中被重复调用,但我只想运行newmessage.text.split一次。我只是把它放在init中,但在游戏中,我在不同的时间更改字符串,所以每次更改字符串时都需要拆分字符串。
我试着做
if self.counter <=1:
words = newmessage.text.split(' ')
但这不起作用(我不知道为什么)。关于如何更好地实现我想要做的事情的任何建议?
class NewLabel(ButtonBehavior, Label):
def __init__(self, **kwargs):
super(NewLabel, self).__init__(**kwargs)
self.font_name='PressStart2P.ttf'
self.font_size=16
self.text=kwargs['text']
self.text_size=(Window.width*.70, Window.height*.23)
self.mipmp = True
self.line_height=1.5
self.markup = True
self.counter=0
#self.words = self.text.split(' ')
def eachwordprint(self, *args):
self.counter += 1
if self.counter <=1:
words = newmessage.text.split(' ')
print "counter: ", self.counter
print "word length", len(words)
if self.counter <= 1:
anim = Animation(size_hint=(1, .25), duration=1.8)
anim.start(messagebox)
self.text=''
if len(words) > self.counter:
self.text += words[self.counter]
self.text += " "
else:
anim2 = Animation(size_hint=(1, 0), duration=1.8)
anim2.start(messagebox)
#messagebox.remove_widget(self)
return False
newmessage = "this is a test hello. this is a test."
“这不起作用”以什么方式?它不应该是'self.words'吗? – ooga
你确定计数器变量不断增加? – aIKid
你确定这是实际的代码吗? 'newmessage.text.split('')'无效,字符串没有'text'属性。 'newmessage'意思是'NewLabel'的一个实例吗? –