2015-11-07 93 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

相关问题