2015-05-04 122 views
0

我想製作我的第一個Kivy遊戲,其中敵人在屏幕上運行,玩家應該點擊它們殺死敵人。 我創建了一個Enemy類,它是級別類的一部分,都是類Widget的子類。我做了一個函數,它自動將類敵人的實例添加到課程級別。我在Enemy類中製作了一張if環,它應該檢查敵人是否到達屏幕的盡頭。 然後它應該從變量zicie中移除一個數字,然後它應該移除敵人,但這兩件事情都不起作用。Kivy Widget不接受屬性和命令

錯誤消息是:

File "bbgsa1.py", line 47, in Update 
    self.parent.remove_widget(self) 
AttributeError: 'NoneType' object has no attribute 'remove_widget' 

File "bbgsa1.py", line 45, in Update 
    self.parent.zicie = self.parent.zicie - 1 
AttributeError: 'NoneType' object has no attribute 'zicie' 

下面是有錯誤的代碼的一部分:

class level(Widget): 
    zicie = NumericProperty(10)# the variable containg the life of the player 
    zloto = NumericProperty(0) 
    e_killed = NumericProperty(0) 
    intv1 = NumericProperty(2/1.) 
    def __init__(self, **kwargs): 
     super(level, self).__init__(**kwargs) 
     Clock.schedule_interval(self.Update, self.intv1) 
    def Update(self, *args):# this funktion generates enemys 
     obj = ROOT.ids.place.ids.level 
     obj.add_widget(Enemy(pos=(800,100))) # the widget enemy is added here 

class Enemy(Widget): 
    imgp = StringProperty('enemy.png') 
    velocity = NumericProperty(1) 
    intv = NumericProperty(0/60.) 
    def __init__(self, **kwargs): 
     super(Enemy, self).__init__(**kwargs) 
     Clock.schedule_interval(self.Update, self.intv) 

    def Update(self, *args):# the funktion that lets the enemy move 
     self.x -= self.velocity 
     if self.x < 1:# checks if the enemy widget reached the end 
      self.velocity = 0#m akes the enemy stop moving 
      self.parent.zicie = self.parent.zicie - 1# the variable zicie that is not found 
      self.parent.remove_widget(self) # this command is also not working 

    def on_touch_down(self, touch):# the funktion, that lets the enemy die 
     if self.collide_point(*touch.pos): 
      self.velocity = 0 
      self.imgp = 'enemyd.png' 
      self.parent.e_killed += 1 
      self.parent.zloto += 10 
      self.parent.remove_widget(self) 

回答

1

這些錯誤是因爲self.parent是沒有線在哪裏運行。我沒有詳細檢查,但是可能會出現這種情況的一種方式是,即使在敵方已從其父母移除後,也會調用時鐘預定self.Update函數。

從屏幕上移除敵人後(爲了避免構建預定功能的數量),您應該小心地取消調度功能,但也可以在嘗試之前通過檢查self.parent是否直接解決問題根據其價值做事。

+0

我試圖用:self.unschedule(self.Update),但它沒有工作 – Gilgamesch

+0

好吧,我明白了,我是一個白癡XD謝謝 – Gilgamesch