Attribute Error

Hi, I just started learning panda3d and got this strange error:

C:\Documents and Settings\USER\Desktop\New Folder (2)>python main.py
DirectStart: Starting the game.
Known pipe types:
  wglGraphicsPipe
(all display modules loaded.)
Traceback (most recent call last):
  File "main.py", line 47, in <module>
    g = World()
  File "main.py", line 14, in __init__
    self.accept('arrow_right-up', self._moveSnakeRight())
  File "main.py", line 40, in _moveSnakeRight
    pos_x = self.ball.getX()
AttributeError: World instance has no attribute 'ball'

Here is my code:


import direct.directbase.DirectStart
from direct.showbase.DirectObject import DirectObject
from direct.task.Task import Task
from pandac.PandaModules import *


class World(DirectObject):

      def __init__(self):
          base.disableMouse()    
          
          self.accept('arrow_right-up',self._moveSnakeRight())
          
          self.loadModels()
      
    
      def loadModels(self):
          self.ball = loader.loadModel('media/untitled.x')
          self.ball.reparentTo(render)
                  
          self.floor = loader.loadModel('media/floor.x')
          self.floor.reparentTo(render)
    
          self.ball.setScale(0.65, 0.65, 0.65)
          self.ball.setPos(0.7, 30, 0.7)
                        
          self.floor.setScale(0.25, 0.25, 0.25)
          self.floor.setPos(0, 30, 0)
          
          self.floor.setHpr(0, 90, 0)
       
        
      def _moveSnakeRight(self): 
          pos_x = self.ball.getX() 
          pos_y = self.ball.getY()
      
          self.ball.setPos(pos_x, pos_y+1, 0)
      
g = World()
    
run()

What’s the problem? Thanks :slight_smile:

Hi, welcome to the forums! :slight_smile:

This is the problem:

self.accept('arrow_right-up',self._moveSnakeRight()) 

You’re calling _moveSnakeRight at that point and passing the return value (None, as you’re not returning anything) to the accept() function. You shouldn’t call the function, so remove the parentheses, like this:

self.accept('arrow_right-up',self._moveSnakeRight) 

Thanks, it worked. :slight_smile: