任何人都可以帮助我在碰撞回调延迟后删除形状和身体。
使用cpSpaceAddPostStepCallback(sapce,(cpPostStepFunc)postStepRemove,blockShape,NULL); 我可以安全地删除形状。 但在调用函数之前我需要一些延迟。 一旦检测到碰撞,我就播放动画。 在代码的最后我需要删除形状。 有人可以帮我一些示例代码。 我写的代码如下。
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;
}
任何人都可以帮我一些示例代码。 谢谢
答案 0 :(得分:1)
我没试过这个,但它应该有效。在碰撞处理程序中,将动画作为序列的一部分启动,然后将postStepCallback作为序列中的最后一项执行:
CCSequence *finalSeq = [CCSequence actions:[CCAnimate actionWithAnimation:sapusAnim restoreOriginalFrame:NO],
[CCCallFunc actionWithTarget:self selector:@selector(removeCpShapeAndBody)], nil];
[sapusSprite1 runAction:finalSeq];
然后你将创建一个名为removeCPShapeAndBody的方法,它将调用cpSpaceAddPostStepCallback。理想情况下,你可以在一个对象中拥有所有这些,这个对象可以保存对形状和主体的引用(例如CCSprite的子类),因此你不必传递它们。