我一直试图在类中的另一个函数内调用一个函数。这导致了错误。我该怎么做才能解决这个问题。代码如下:
class Goomba:
def __init__(self,x,y):
self.x = x
self.y = y
def goomleft(self,speed):
for i in range(speed):
if mask.get_at((self.x,self.y+10))[0] !=255:
self.x-=1
def goommove(self,direction):
if direction == 'left':
goomleft(self,3) #this is where I called it
错误说 NameError:全球名称' goomleft'未定义
答案 0 :(得分:0)
你必须在彼此内部调用函数,因为类中的函数范围是不同的:
class Goomba:
def __init__(self,x,y):
self.x = x
self.y = y
def goommove(self,direction):
def goomleft(self,speed):
for i in range(speed):
if mask.get_at((self.x,self.y+10))[0] !=255:
self.x-=1
if direction == 'left':
goomleft(self,3) #this is where I called it