The 2 problems I encounter are:
- Sometimes I crash in my update funciton on a setPosition call to my sprites (I think from trying to access body->p)
- Sometimes when I change levels, blocks from the previous level aren't free'd, though all their dealloc's are being called (not directly by me of course)
Code: Select all
@interface Block : NSObject
{
// Physics
... (mass, friction, etc)...
cpBody* body;
cpShape* shape;
// Graphics
Sprite* sprite;
Sprite* arrow;
}
@end
@implementation Block
- (Block*)initWithShape:Alignment:Fixed:X:Y:W:H:Mass:Friction:Elasticity:Rotation:Direction:Layer
{
/* setup sprite */
// SQUARE
if(shapeName isEqualToString:@"Square")
{
cpVect vertices[] =
{
cpv(-w/2, -h/2),
cpv(-w/2, h/2),
cpv( w/2, h/2),
cpv( w/2, -h/2),
};
if(fixed)
body = cpBodyNew(INFINITY, INFINITY);
else
body = cpBodyNew(mass, cpMomentForPoly(mass, 4, vertices, cpvzero));
body->p = cpv(x,y);
shape = cpPolyShapeNew(body, 4, vertices, cpvzero);
}
/* else if circle, etc... */
shape->e = elasticity;
shape->u = friction;
shape->collision_type = fixed ? 0 : 1;
}
- (void)dealloc
{
// Free sprites
[scene removeChildByTag:SpriteID cleanup:NO]; // block
[scene removeChildByTag:SpriteID+1000 cleanup:NO]; // arrow
// Remove shape and body from space.
fixed ? cpSpaceRemoveStaticShape(AppDelegate.space, shape) : cpSpaceRemoveShape(AppDelegate.space, shape);
if(!fixed) cpSpaceRemoveBody(AppDelegate.space, body);
// Free phsyics memory.
cpShapeFree(shape);
cpBodyFree(body);
shape = nil;
body = nil;
// Free strings
...
SpriteID--;
[super dealloc]
}
@end
I know that I can't access those bodies anymore in my update function, so they are not in my array of Block*'s anymore. Here is my update function if it helps. I've really piled on the error checking in my attempts to resolve this:
Code: Select all
- (void)UpdateBlock:(Block*)block
{
if(block == nil || ActiveBlocks == nil) // Activeblocks is NSMutableArray
return;
cpShape* shape = block.shape;
cpBody* body = block.body;
Sprite* sprite = block.sprite;
Sprite* arrow = block.arrow;
if(shape== nil || body == nil || sprite == nil)
return;
// Block image
if(sprite)
{
[sprite setRotation:CC_RADIANS_TO_DEGREES(-(body->a))];
[sprite setPosition:cpv(body->p.x, body->p.y)];
}
// Arrow image
if(arrow)
{
[arrow setRotation:CC_RADIANS_TO_DEGREES(block.arrowRotation)];
[arrow setPosition(cpv(body->p.x, body->p.y)];
}