2014-04-08 34 views
-1

所以作为游戏的一部分,我有一个动画文本窗口,可以弹出每秒打印每个单词。为什么我只能在我的方法中只运行一行?

这里的问题是方法“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." 
+0

“这不起作用”以什么方式?它不应该是'self.words'吗? – ooga

+0

你确定计数器变量不断增加? – aIKid

+0

你确定这是实际的代码吗? 'newmessage.text.split('')'无效,字符串没有'text'属性。 'newmessage'意思是'NewLabel'的一个实例吗? –

回答

0

你可以memoize的分裂

MC = {} 


def wrapper(fn): 
    def inner(arg): 
     if not MC.has_key(arg): 
      MC[arg] = fn(arg) 
     return MC[arg] 
    return inner 

@wrapper 
def myfn(x): 
    print x 

myfn(1) 
myfn(1) 
myfn(2) 

所以基本上输出发生了什么是你写你的文字划分功能这样

@wrapper 
def split(text): 
    return text.split(' ') 

,每次你想NewMessage作为的分裂。文本,你称它分裂(newmessage.text)。包装器将首先检查文本是否已经遇到,如果是,则返回值,或者调用该函数并拆分并存储它以备后用。

可以运行如果这里http://codebunk.com/b/-JJzVKR8KgtdaIIsmODh

0

上面的代码你newmessage正在由不需要了解NewLabel比如你代码的其他部分修改,我可能会创建一个类来封装newmessage行为:

class Message(object): 
    def __init__(self, message): 
     self.set_text(message) 
    def set_text(self, message): 
     self.text = message.split(' ') 

newmessage = Message("this is a test hello. this is a test.") 

然后把它作为传递给NewLabel一个参数:

class NewLabel(ButtonBehavior, Label): 
    def __init__(self, **kwargs): 
     ... 
     self.message = kwargs['message'] 

    def eachwordprint(self, *args): 
     words = self.message.text 
     print "counter: ", self.counter 
     print "word length", len(words) 
     ... 

每当你需要改变的消息,你会怎么做:

newmessage.set_text('this is the new test') 

不过,如果你总是设置在知道你NewLabel部分代码的消息文本,那么就将它们直接添加到NewLabel类中:

class NewLabel(ButtonBehavior, Label): 
    def __init__(self, **kwargs): 
     ... 
     self.set_text(kwargs['message']) 

    def set_text(self, text): 
     self.words = text.split(' ') 

    def eachwordprint(self, *args): 
     words = self.words 
     print "counter: ", self.counter 
     print "word length", len(words) 
     ... 
相关问题