Yet another problem....... :(

Hi every1 … the code is the following 1:

i have multiple .py files:

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

from TrackClass_01 import Track

from CycleClass_01 import Cycle

class World(DirectObject):
	def __init__(self):
	 base.setBackgroundColor(0, 0, 0)
	 dt = globalClock.getDt()
	 taskMgr.doMethodLater(10, self.debugTask, "Debug Task")
	 self.accept("w", self.setKey, ["w", True])
	 self.accept("s", self.setKey, ["s", True])
	 self.accept("a", self.setKey, ["a", True])
	 self.accept("d", self.setKey, ["d", True])
	 self.accept("w-up", self.setKey, ["w", False])
	 self.accept("s-up", self.setKey, ["s", False])
	 self.accept("a-up", self.setKey, ["a", False])
	 self.accept("d-up", self.setKey, ["d", False])
	 self.accept("mouse1", self.setKey, ["mouse1", True])
	 self.accept("mouse3", self.setKey, ["mouse3", True])
	 self.accept("mouse1-up", self.setKey, ["mouse1", False])
	 self.accept("mouse3-up", self.setKey, ["mouse3", False])
	def debugTask(self, task):
		taskMgr.removeTasksMatching("Cycle Move *")
		return task.again

then: CycleClass:

from direct.showbase.DirectObject import DirectObject

class Cycle(DirectObject):
	def __init__(self):
	 self.speed = 0
	 self.throttle = 0
	 self.maxSpeed = 200
	 self.accel = 25
	 self.handling = 20
	 taskMgr.add(self.cycleControl, "Cycle Control")
	 self.keyMap = {"w" : False,
		"s" : False,
		"a" : False,
		"d" : False,
		"mouse1" : False,
		"mouse3" : False}
	def setKey(self, key, value):
		self.keyMap[key] = value
	def cycleControl(self, task):
		dt = globalClock.getDt()
		if( dt > .20):
			return task.cont
		if(self.keyMap["w"] == True):
			self.adjustThrottle("up", dt)
		elif(self.keyMap["s"] == True):
			self.adjustThrottle("down", dt)
		if(self.keyMap["d"] == True):
		 self.turn("r", dt)
		elif(self.keyMap["a"] == True):
		 self.turn("l", dt)
		if(self.keyMap["mouse1"] == True):
		 self.cameraZoom("in", dt)
		elif(self.keyMap["mouse3"] == True):
		 self.cameraZoom("out", dt)
		if(base.mouseWatcherNode.hasMouse() == True):
		 mpos = base.mouseWatcherNode.getMouse()*30)* -30)
		return task.cont
	def cameraZoom(self, dir, dt):
	 if(dir == "in"):, 10 * dt)
	 else:, -10 * dt)
	def turn(self, dir, dt):
	 turnRate = self.handling * (2 -(self.speed / self.maxSpeed))
	 if(dir == "r"):
	  turnRate = -turnRate
	  self.cycle.setH(self.cycle, turnRate * dt)
	def adjustThrottle(self, dir, dt):
	 if(dir == "up"):
	  self.throttle += .25 * dt
	  if(self.throttle >1):
	   self.throttle = 1
	  self.throttle -= .25 * dt
	  if(self.throttle < -1):
	   self.throttle = -1
	def speedCheck(self, dt):
	 tSetting = (self.maxSpeed * self.throttle)
	 if(self.speed < tSetting):
		if((self.speed + (self.accel * dt)) > tSetting):
			self.speed = tSetting
				self.speed += (self.accel * dt)
	 elif(self.speed > tSetting):
		if((self.speed - (self.accel * dt)) < tSetting):
			self.speed = tSetting
			self.speed -= (self.accel * dt)
	def move(self, dt):
	  mps = self.speed*1000/3600
	  self.cycle.setY(self.cycle, mps * dt)

The problem is, that when i run the world class, i get this:

Known pipe types:
(all display modules loaded.)
Traceback (most recent call last):
File “”, line 35, in
File “”, line 17, in init
self.accept(“w”, self.setKey, [“w”, True])
AttributeError: World instance has no attribute ‘setKey’

I dont quite understand why it dosent import this part… so… help pls?.. Thx And sry foe beeing so persistent … its just that i really wanna learn this!

Your World class does not have a method called setKey. Instead, you have defined setKey() in your Cycle class, which you instance in your World class.

So, correct way would be to do:

self.accept("w", self.cycle.setKey, ["w", True])


I see… indeed now, it works… thx a lot for the info ognjenk :smiley: :slight_smile: