2015-11-07 91 views
1

我試圖遍歷元組的陣列(在恆定形式):「NoneType」對象不是可迭代

SPRITE_RIGHT = [(0, 0), (16, 0), (32, 0)] 
SPRITE_LEFT = [(0, 16), (16, 16), (32, 0)] 
SPRITE_UP = [(0, 32), (16, 32), (32, 0)] 
SPRITE_DOWN = [(0, 48), (16, 48), (32, 0)] 
def symbol(self): 
    self._status += 1 

    if (self._status > 2): 
     self._status = 0 

    if (self._dx > 0): 
     (x, y) = PacMan.SPRITE_RIGHT[self._status] 
     return (x,y) 
    if (self._dx < 0): 
     (x, y) = PacMan.SPRITE_LEFT[self._status] 
     return (x,y) 
    if (self._dy > 0): 
     (x, y) = PacMan.SPRITE_DOWN[self._status] 
     return (x,y) 
    if (self._dy < 0): 
     (x, y) = PacMan.SPRITE_UP[self._status] 
     return (x,y) 
... 
for a in arena.actors(): 
     if not isinstance(a, Wall): 
      x, y, w, h = a.rect() 
      xs, ys = a.symbol()    #This line gives me the problem 
      screen.blit(sprites, (x, y), area=(xs, ys, w, h)) 

當我執行該程序我收到此錯誤:

TypeError: 'NoneType' object is not iterable 

對於每一個演員我調用該方法符號()來獲取其圖像

When i print PacMan.SPRITE_UP[0] for example it returns the correct tuple

+1

您發佈的代碼似乎是正確的,假設'self._status'的合理值。也許你省略了太多。你可以嘗試做一個最小的工作示例,執行時仍顯示錯誤嗎? – Joost

+0

x,y = None @Joost – palsch

+0

所以SPRITE_RIGHT [self._status]是無 – palsch

回答

0

檢查值由a.symbol()返回。它看起來像試圖將它解開爲兩個值並失敗。

當你這樣做:

xs, ys = a.symbol()  #This line gives me the problem 

它調用a.symbol(),它返回一個值。該代碼假定此 值是一個包含兩個值的迭代。 xsys然後將 更改爲對這兩個值的引用。

如果a.symbol()返回的值不是可迭代的,則分配將失敗。您收到的錯誤訊息, TypeError: 'NoneType' object is not iterable,暗示 a.symbol()正在返回None

+0

當我打印PacMan.SPRITE_UP [0]例如它返回正確的元組 – Alex

+0

謝謝,我解決了問題。我沒有考慮dx的特定值 – Alex

相關問題