how to remove shape and body after a delay in collision callback (cocos2d-chipmunk)
can any开发者_如何学运维one help me to remove shape and body after a delay in collision callback.
By using cpSpaceAddPostStepCallback(sapce, (cpPostStepFunc)postStepRemove, blockShape, NULL); i could safely remove the shape . But i need some delay before i call the function. I play an animation once the collision in detected. At the end of the code i need to remove the shape. Can anyone please help me with some sample code. The code that i have written is as follows.
int collisionSapusBlock(cpArbiter *arb, struct cpSpace *sapce, void *data)
{
cpShape *sapusShape, *blockShape;
cpArbiterGetShapes(arb, &sapusShape, &blockShape);
cpBody *BlockBody = blockShape->body;
cpBody *sapusBody = sapusShape->body;
CCNode *parent = (CCNode*)data;
if (cpvlength(sapusBody->v) > 45)
{
NSLog(@"Collision2 called %f",cpvlength(sapusBody->v));
CCSprite *sprite = blockShape->data;
[parent removeChild:sprite cleanup:YES];
///////
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"Block2.plist"];
CCSpriteBatchNode *sheet1 = [CCSpriteBatchNode batchNodeWithFile:@"Block2.png" capacity:2];
[parent addChild:sheet1];
CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache];
CCSprite *sapusSprite1 = [[CCSprite alloc] initWithSpriteFrameName:@"Block2001.png"];
CCAnimation *sapusAnim = [[CCAnimation alloc] initWithName:@"select" delay:0.15f];
[sapusAnim addFrame:[cache spriteFrameByName:@"Block2001.png"]];
[sapusAnim addFrame:[cache spriteFrameByName:@"Block2002.png"]];
[sapusAnim addFrame:[cache spriteFrameByName:@"Block2003.png"]];
[sapusAnim addFrame:[cache spriteFrameByName:@"Block2004.png"]];
[sapusAnim addFrame:[cache spriteFrameByName:@"Block2005.png"]];
[sapusAnim addFrame:[cache spriteFrameByName:@"Block2006.png"]];
[sapusSprite1 addAnimation: sapusAnim];
[sheet1 addChild:sapusSprite1];
CCAnimate *animate = [CCAnimate actionWithAnimation: sapusAnim restoreOriginalFrame:NO];
[sapusSprite1 runAction:animate];
blockShape->data = sapusSprite1;
/////here after playing the animation i need to delete the blockShape. Help plzzz
}
return 1;
}
Can anyone please help me with some sample code. Thanks
I haven't tried this, but it should work. In the collision handler, kick off the animation as part of a sequence and then execute the postStepCallback as the last item in the sequence:
CCSequence *finalSeq = [CCSequence actions:[CCAnimate actionWithAnimation:sapusAnim restoreOriginalFrame:NO],
[CCCallFunc actionWithTarget:self selector:@selector(removeCpShapeAndBody)], nil];
[sapusSprite1 runAction:finalSeq];
Then you'd create a method called removeCPShapeAndBody which would call cpSpaceAddPostStepCallback. Ideally you'd have all of this in an object that would hold reference to the shape and body (e.g. a subclass of CCSprite) so you don't have to pass those around.
精彩评论