我有一个名为sprite的CCSprite,它是一个名为movingLayer的CCLayer的子代,它本身就是运行我的游戏逻辑的当前CCLayer的子代,所以在这种情况下它是自我的. movingLayer在屏幕上来回移动,重复永远的
当我尝试这样做时,我需要告诉movingLayer删除sprite和self来添加精灵.这就是我做的……
- (void)attack:(id)sender { [sprite stopAllActions]; [movingLayer removeChild:sprite cleanup:YES]; [self addChild:sprite z:1]; CGFloat distance = ccpDistance(sprite.position, ccp(sprite.position.x, -sprite.contentSize.height/2)); id goDown = [CCMoveTo actionWithDuration:distance/moveDuration position:ccp(sprite.position.x, -sprite.contentSize.height/2)]; id repeatDown = [CCRepeatForever actionWithAction:[CCSequence actions:[CCMoveTo actionWithDuration:0 position:ccp(sprite.position.x, sprite.contentSize.height/2+self.contentSize.height)], [CCMoveTo actionWithDuration:moveDuration position:ccp(sprite.position.x, -sprite.contentSize.height/2)], nil]]; id attackAction = [CCSequence actions:goDown, repeatDown, nil]; [sprite runAction:attackAction]; }
我认为stopAllActions是多余的,但这不是问题.如果我在moveLayer中删除sprite,然后将其添加到self我崩溃以访问僵尸,如果我尝试将其添加到自己的第一个,我崩溃尝试添加已经添加的东西.
你有没有尝试过清理NO?或者,在从movingLayer中删除sprite之前尝试保留sprite,然后在完成后释放它:
[sprite retain]; [movingLayer removeChild:sprite cleanup:YES]; [self addChild:sprite z:1]; [sprite release];