I recently switched to spritesheets and now have problems with collisions. My objects extend the cpCCSprite class like so :
@interface Boss : cpCCSprite {
}
I have the init method like this :
-(Boss * ) initWithCPBody: (cpShape *) mainBody withManager:(SpaceManager*)spaceManager {
if(self != nil){
self = [Boss spriteWithShape:mainBody texture:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"spiderBoss1.png"].texture];
[self setAutoFreeShape:YES];
[self setSpaceManager:spaceManager];
frameCount = 0;
NSMutableArray *animFrames = [NSMutableArray array];
for(int i = 1; i < 15; i++) {
CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"spiderBoss%d.png",i]];
[animFrames addObject:frame];
}
CCAnimation *animation = [CCAnimation animationWithName:@"forwardAnimation" delay:0.2f frames:animFrames];
[self runAction:[CCRepeatForever actionWithAction: [CCAnimate actionWithAnimation:animation restoreOriginalFrame:NO] ]];
}
return self;
}
It's a shooting game so I have a collision handler between bullets and enemies ( bosses ). The bullets are declared like so :
cpShape *bullet = [smgr addRectAt:gunPos mass:5 width:5 height:5 rotation:-CC_DEGREES_TO_RADIANS(soldierAngle)];
bullet->collision_type = kBulletCollisionType;
cpCCSprite *ballS= [ cpCCSprite spriteWithShape:bullet file:@"bullet.png"];
and so on..
however in the collision which is below I get a EXC_BAD_ACCESS
on line cpCCSprite* shapeSprite = (cpCCSprite*)shape->data; in the following collision handler. I don't get any information. Is there somthing I need to do different?
- (int) handleCollisionWithRect:(cpShape*)shape
enemy:(cpShape*)enemy
contactPts:(cpContact*)contacts
numContacts:(int)numContacts
normalCoef:(cpFloat)coef
{
//the enemy
Boss* sprite = (Boss*)enemy->data;
[sprite decreaseEnergy:50];
if ( [sprite getEnergy] == 0 )
{
if (sprite)
{
[sprite.parent removeChild:sprite cleanup:NO];
[smgr scheduleToRemoveAndFreeShape:enemy];
enemy->data = nil;
}
}
//the bullet
//ERROR HERE
cpCCSprite* shapeSprite = (cpCCSprite*)shape->data;
if (shapeSprite)
{
[shapeSprite.parent removeChild:shapeSprite cleanup:NO];
[smgr scheduleToRemoveAndFreeShape:shape];
shape->data = nil;
}
return 1;
}
thanks for the help