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]