Ok, I have changed my code to involve classes now. I have a penguin class which contains the collision traverser, the handler, and accepts the collision Entry events. Here’s the code for that:
# create a collision segment coming out of the penguin's front
self.penguinSeg = CollisionSegment(0,0,1,0,10,1)
self.penguinSegNodePath = self.model.attachNewNode(CollisionNode('penguinColSegNode') )
self.penguinSegNodePath.node().addSolid(self.penguinSeg)
self.penguinSegNodePath.show()
# set up collision traverser
self.traverser = CollisionTraverser('penguinTraverser')
base.cTrav = self.traverser
# initialize collision handler
self.collisionHandler = CollisionHandlerEvent()
# check if the from object hit any node
self.collisionHandler.addAgainPattern('%fn-again-%in')
for i in range (1, 500):
self.accept('self.penguinColSegNode-again-rock'+str(i), w.rocks[i].collide, [w.rocks[i], w])
self.accept('self.penguinColSegNode-again-spike'+str(i), w.spikes[i].collide, [w.rocks[i], w])
# add the collision node path and the handler to the traverser
self.traverser.addCollider(self.penguinSegNodePath, self.collisionHandler)
Basically i have a dictionary of all the rocks and spikes in my level, with the keys being ints from 1-500 and the values being the rock/spike object itself (or a pointer to the object if you like).
Panda doesnt seem to like these accept statements. It says "World instance has no attribute ‘rocks’ ". But I passed the world class to the penguin class when I declared the penguin, as follows:
self.penguin = Penguin(self.penguinModel, self)
and I look for the world instance in my init method in the penduin class as follows:
class Penguin(DirectObject):
def __init__(self, model, w):
So I’m sitting here looking at the following 2 lines of code in the init function of my world class:
self.rocks = {}
self.spikes = {}
My question is: Why is the penguin class not recognizing the w.rocks and w.spikes dictionaries when I pass the world instance w to the penguin class init function?