所以作爲遊戲的一部分,我有一個動畫文本窗口,可以彈出每秒打印每個單詞。爲什麼我只能在我的方法中只運行一行?
這裏的問題是方法「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'的一個實例嗎? –