no movement

this is the updated version from last night
Hi everyone i need some help with the event handler and how to move an object across the bottom of the screen form right to left and left to right
i have taken sample code form roaming ralph and the solar systems examples
please have a look at the code

import direct.directbase.DirectStart
from direct.showbase.DirectObject import DirectObject
from direct.task.Task import Task
from direct.gui.OnscreenText import OnscreenText
from pandac.PandaModules import TextNode

def genLabelText(text, i):
  return OnscreenText(text = text, pos = (-1.3, .95-.05*i), fg=(1,1,0,1),
                      align = TextNode.ALeft, scale = .05)
class World(DirectObject):
    def __init__(self):
       
        self.title = OnscreenText(text="Spaced Balls the game",
                              style=1, fg=(1,1,0,1),
                              pos=(0.8,-0.95), scale = .07)
        self.LeftkeyText =   genLabelText("[a:] to go Left", 0)
        self.RightkeyText =  genLabelText("[l:] to go Right", 1)
        
        self.solar = loader.loadModel("models/solar_sky_sphere")
        self.solar.reparentTo(render)
        self.solar.setPos(0,0,-5)
        
        self.solarsun = loader.loadModel("models/planet_sphere")
        self.solarsun.reparentTo(render)
        self.solarsun.setPos(2,15,0)
        
        base.setBackgroundColor(0, 0, 0)    #Set the background to black
        base.disableMouse()
        
        self.Key = {"Left":0, "Right":0}
      
        
        # Accept the control keys for movement
        self.accept("a", self.setKey, ["Left",1])
        self.accept("a-up",  self.setKey, ["Left", 0])
        self.accept("l", self.setKey, ["Right",1])
        self.accept("r-up", self.setKey, ["Right",0])

        taskMgr.add(self.move,"moveTask")
       
   
   
   #Records the state of the arrow keys
    def setKey(self, key, value):
        self.key[key] = value
    
    
    # Accepts arrow keys to move either the player    
    def move(self, task):
     return Task.cont
              
   # Get the time elapsed since last frame. We need this
        # for framerate-independent movement.
     elapsed = globalClock.getDt()
    def Key(self, key):
       
  # If a move-key is pressed, move sun in the specified direction.
     if (self.key["Left"]!=0):
            self.solarsun.setX(self.solarsun, -(elapsed*25))
            
     if (self.key["Right"]!=0):
            self.solarsun.setZ(self.solarsun, -(elapsed*25))
     return Task.cont  
     
       
  
  
w = World()
run()

method (*(extraArgs + sentArgs))
File “audio1.1.py”, line 45, in setKey
self.key[key] = value
AttributeError: World instance has no attribute ‘key’

so why does the planet not move when i press “a” or “l”
am i missing some code?

[img][/img]

Use

[code]
it will really help.

In the code you posted I don’t see a definition of ‘move’ and ‘gameLoop’ functions that you add to the task manager.

I think that I see a few issues:

  1. First, as to your error message:
self.Key = {"Left":0, "Right":0} 
self.key[key] = value 

Capitalisation matters, I believe: you’ve defined “self.Key” to be capitalised, but are attempting to use it as “self.key”; in short, “self.Key” and “self.key” are two different names as far as Python is concerned.

self.accept("l", self.setKey, ["Right",1])
self.accept("r-up", self.setKey, ["Right",0]) 

You’re accepting presses of ‘l’ and releases of ‘r’; I presume that it should be one or the other in both cases.

# Accepts arrow keys to move either the player
def move(self, task):
return Task.cont 

You seem to be immediately returning from the “move” method, meaning that any code below that return statement within “move” will presumably not be executed.

def Key(self, key):

What are you attempting to do here?

You’ve previously defined “self.Key” to be the dictionary that holds your keys, but here define it as a method. Further, it appears to hold your movement code, while the “move” method appears to be the method actually being called.

hey there thanks for the reply i think i have corrected the problem however when i run this code i just get a blank screen ???
so what am i doing now?

from direct.showbase.DirectObject import DirectObject
from direct.task import Task
import direct.directbase.DirectStart

class Movingtest (DirectObject):
def init(self):

    self.environ = loader.loadModel("models/solar_sky_sphere")      
    self.environ.reparentTo(self.render)
    self.environ.setPos(0,0,0)

    self.Sun = loader.loadModel("models/planet_sphere")
    self.Sun.reparentTo(self.render)
    self.Sun.setPos(2,15,0)

    base.setBackgroundColor(0, 0, 0)    #Set the background to black
    base.disableMouse()
    camera.setPos ( 0, 0, 45 )          #Set the camera position (X, Y, Z)
    camera.setHpr ( 0, -90, 0 )         #Set the camera orientation
                                    #(heading, pitch, roll) in degrees


    self.state = {"left"  :False,
             "right" :False
                }
                
    taskMgr.add(self.movement, "Moving Sun")

  #capture keyboard events#
    self.accept("a", self.changeState ["left", True])
    self.accept("a_left-up", self.changeState, ["left",False])
    self.accept("l", self.changeState, ["right",True])
    self.accept("l_right-up", self.changeState, ["right",False])

def movement(self, task):
  if(self.state["left"]):
   self.Sun.setX( self.Sun.getX() - 0.5 )
  elif(self.state["right"]):
   self.Sun.setX( self.Sun.getX() + 0.5 )
   return Task.cont

def changeState(self, key, value):
    self.state[key] = value

#m = Movingtest()
#m.run()
run()

First of all, please use code tags when posting code; it makes your code rather easier to read, making it easier to help you and people more likely to attempt to do so.

The appropriate tags are used like so (in the message composition/editing box):

[code]
class Cat():
    def CatMethod(self):
        #Note that indentation is preserved,
        # making the code more legible.
        print "Mew!"

    def AnotherCatMethod(self):
        print "Purr!"
[/code]

Which should produce the following:

class Cat():
    def CatMethod(self):
        #Note that indentation is preserved,
        # making the code more legible.
        print "Mew!"

    def AnotherCatMethod(self):
        print "Purr!"

As to your problem, at a quick glance I’m inclined to guess that you’ve simply placed the camera at a position from which it doesn’t see anything. Try changing its HPR (perhaps try 90 instead of -90 for the “P”, for example).

If that doesn’t help, and no-one else has any suggestions, put your code into code tags (you should be able to edit your post above) and I’ll likely take another, closer look. :slight_smile: