2017-01-10 9 views
0

큰 리셋 버튼으로 타이머를 만들고 싶지만 타이머 노드 (n)의 현재 값을 표시하는 레이블 노드를 얻는 방법을 모르겠습니다.Pythonista 3의 Labelnode 타이머

답변을 찾으려고했지만 아무 도움도없이 도움을 얻을 수있었습니다.

from scene import * 
    import sound 
    import random 
    import math 
    import time 
    A = Action 

    class MyScene (Scene): 
     def setup(self): 
     n = 20 
     Scene.background_color = 1.0, 1.0, 1.0 
     self.button = SpriteNode('IMG_4056.PNG') 
     self.button.position = (512, 400) 
     self.add_child(self.button) 
     self.time = LabelNode(str(n), font = ('courier', 50)) 
     self.time.position = (512, 100) 
     self.add_child(self.time) 
     self.life = LabelNode("Life", font = ('courier', 50)) 
     self.life.position = (512, 700) 
     self.add_child(self.life) 
     pass 


def did_change_size(self): 
    pass 

def update(self): 
    pass 

def touch_began(self, touch): 
    if touch.location in self.life.bbox: 
     n = 20 
     while (n >= 0): 
      self.time.remove_from_parent() 
      self.time = LabelNode(str(n - 1)) 
      self.add_child(self.time) 
      time.sleep(1) 
    pass 

def touch_moved(self, touch): 
    pass 

def touch_ended(self, touch): 
    pass 

    if __name__ == '__main__': 
     run(MyScene(), show_fps=True) 

답변

0

다음은 타이머의 샘플 구현이며 도움이 되길 바랍니다.

import scene 

def custom_action(node, progress): 
    ntime = node.parent.duration 
    i = ntime - int(ntime*progress) 
    if i >= 0: 
     node.text = str(i) 

class MyScene(scene.Scene): 
    def setup(self): 
     self.test_label = scene.LabelNode('0', 
      position=self.size/2.0, parent=self) 
     self.duration = 10 
     self.activate_timer_label = scene.LabelNode('Activate Timer', 
      position=(self.size[0]/2-100, self.size[1]/2-100), 
      parent=self) 
     self.stop_timer_label = scene.LabelNode('Stop Timer', 
      position=(self.size[0]/2+100, self.size[1]/2-100), 
      parent=self) 

    def touch_began(self, touch): 
     if touch.location in self.stop_timer_label.frame: 
      animate_action = scene.Action.call(custom_action, self.duration) 
      self.test_label.remove_action('timer_action') 
      self.test_label.text = '0' 
      return 
     if touch.location in self.activate_timer_label.frame: 
      timer_action = scene.Action.call(custom_action, self.duration) 
      self.test_label.run_action(timer_action, 'timer_action') 

scene.run(MyScene())