How to use cocos2d collision_model

I recently used cocos2d

s Python

to make a game. But I had problems with collision_model

. I have read the documentation but I still cannot use CollisionManager

to add CollidableSprite

like in.

I actually had to change ActorSprite

to CollidableSprite

to avoid the error. What is the problem?

import cocos.euclid as eu
import cocos.collision_model as cm

class CollidableSprite(cocos.sprite.Sprite):
    def __init__(self, image, center_x, center_y, radius):
        super(ActorSprite, self).__init__(image)
        self.position = (center_x, center_y)
        self.cshape = cm.CircleShape(eu.Vector2(center_x, center_y), radius)

class ActorModel(object):
    def __init__(self, cx, cy, radius):
        self.cshape = cm.CircleShape(eu.Vector2(center_x, center_y), radius)

      

The documentation showed this example initializing a colliding sprite. But where does it come from ActorSprite

? I have to change it to CollidableSprite

in order to get the class to work. And I tell the collision manager to add the sprites. But it returns None

when I call the function CollisionManeger.known_objs()

.

 def __init__(self):
    super(page,self).__init__()
    self.collision_manager = CollisionManager()
    self.collision_manager.add(self.sprite1)
    self.collision_manager.add(self.sprite2)
    print self.collision_manager.known_objs()

      

So, is there something wrong with the documentation? Or have I misunderstood this?

+3


source to share


1 answer


It looks like the example should actually initialize CollidableSprite

instead ActorSprite

.

CollisionManager

a class is just an interface. He does not do anything. In collision_model : CollisionManagerBruteForce

and CollisionManagerGrid

. Therefore you can use for example:



self.collision_manager = CollisionManagerBruteForce()

      

but note that it is CollisionManagerGrid

more efficient .

+2


source







All Articles