i have many “peg” objects that do not move, and have a method called hit()
...
pegShape->data = this; // pointer to this Peg object
pegShape->collision_type = 2; // Collisions are grouped by types
i also have a ball that can collide with these pegs
...
ballShape->data = this;
ballShape->collision_type = 1; // Collisions are grouped by types
i’ve added a collision handler
cpSpaceAddCollisionHandler(m_pSpace,1,2,&spriteCollision,NULL,NULL,NULL,this);
static int spriteCollision(cpArbiter *arb, cpSpace *space, void *nothing) {
CP_ARBITER_GET_SHAPES(arb, a, b);
Peg* shapeDataB = (Peg*) b->data;
shapeDataB->hit();
}
it crashes with this error Thread 1: EXC_BAD_ACCESS (code=2, address=0xbfffda…
i’m pretty sure my cast is bad. though the object is not NULL.
here is my full Peg class. it just holds a sprite and has a method to hit();
Peg::Peg(CCPoint pos)
{
sprite = CCSprite::createWithSpriteFrameName("blue");
Graphics::getInstance().m_pPegSBN->addChild(sprite);
sprite->autorelease();
sprite->setPosition(pos);
float pegRadius = 13.0f; // should be 10
cpShape *pegShape = cpCircleShapeNew(pachiBoard->m_pSpace->staticBody, pegRadius, cpv(pos.x,pos.y));
pegShape->e = 0.5; // Elasticity
pegShape->u = 0.8; // Friction
pegShape->data = this; // Associate with ths peg
pegShape->collision_type = 2; // Collisions are grouped by types
// Add the shape to out space
cpSpaceAddStaticShape(pachiBoard->m_pSpace, pegShape);
}
void Peg::hit()
{
CCLog("peg is hit");
}
Screen Shot 2013-02-13 at 6.26.00 PM.png (73.4 KB)
ss0213.png (73.4 KB)