You need to indent these lines:
if self.currentState2==1:
elif self.currentState2==0:
They are not considered part of the function def since they have the same indentation as the function name.
Question
So I'm getting this error when I compile, I don't understand though because I've got 2 identical classes, one works fine the other throws out this error.
if self.currentState2==1: NameError: name 'self' is not defined
class EnemyShip(pygame.sprite.Sprite):
def __init__(self, (x, y), playerShip):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("enemy.png")
self.image = self.image.convert()
tranColor = self.image.get_at((1, 1))
self.image.set_colorkey(tranColor)
self.rect = self.image.get_rect()
self.enemyX = float(x)
self.enemyY = float(y)
self.ship = playerShip
self.count = 0
self.currentState2 = 0
def update(self):
self.count += 1
x_component = self.ship.rect.centerx-self.enemyX
y_component = self.ship.rect.centery-self.enemyY
distance = math.hypot(x_component, y_component)
if distance < 100:
self.currentState2 = 1
print distance
elif distance > 100:
self.currentState2 = 0
print "test2"
if self.count < 600:
self.caculateNextPosition()
self.rect.center = (self.enemyX, self.enemyY)
elif self.count < 600:
self.caculateNextPositionEvade()
self.rect.center = (self.enemyX, self.enemyY)
else:
self.count = 0
def caculateNextPosition(self):
shipX = self.ship.rect.centerx
shipY = self.ship.rect.centery
if self.currentState2==1:
if self.enemyY < shipY:
self.enemyY += 4
elif self.enemyY > shipY:
self.enemyY -= 4
if self.enemyX < shipX:
self.enemyX += 4
elif self.enemyX > shipX:
self.enemyX -= 4
elif self.currentState2==0:
if self.enemyY < shipY:
self.enemyY += 0
elif self.enemyY > shipY:
self.enemyY -= 0
if self.enemyX < shipX:
self.enemyX += 0
elif self.enemyX > shipX:
self.enemyX -= 0
Solution 2
You need to indent these lines:
if self.currentState2==1:
elif self.currentState2==0:
They are not considered part of the function def since they have the same indentation as the function name.
OTHER TIPS
Your indentation is incorrect in the method calculateNextPosition
. As a result, the series of if statements are executed in the body of the class (not the method), where self
isn't defined.
Your indentation is off. The if statement is outside of the method you think it's in.
Check your indentation - the condition is on the same level as your function definition. In other words it's not inside the function so technically yes - self is not defined.
You might just have an indentation error.
Since self
is implicitly passed as the first argument to an instance method, if you have something like:
class A(object):
def foo(self):
return "foo called"
...then it's fine, because self
is a local variable for the method. However, if you goof up the indentation like this:
class A(object):
def bar(self):
if self.x == 1:
do_stuff()
if self.x == 2:
do_something_else()
...that second self.x
will cause the error, because self
doesn't mean anything outside the scope of the function.