How do I remove sprites that are on top of each one but with different z order?
The code that I'm using is:
- (void)removeSelectedSprite:(CGPoint)touchLocation {
CCSprite * newSprite = nil;
for (CCSprite *sprite in selectedSpritesArray) {
if (CGRectContainsPoint(sprite.boundingBox, touchLocation)) {
newSprite = sprite;
break;
}
}
if (newSprite) {
CCSprite *fixedSprite = [CCSprite spriteWithSpriteFrameName:@"Animation_01.png"];
fixedSprite.position = ccp(newSprite.contentSize.width/2,newSprite.contentSize.height/2);
[newSprite addChild:fixedSprite];
NSMutableArray *animFrames = [NSMutableArray array];
for(int i = 1; i <= 5; ++i) {
[animFrames addObject: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"Animation_%02d.png", i]]];
}
CCAnimation *anim = [CCAnimation animationWithFrames:animFrames delay:0.05f];
CCActionInterval *animAction = [CCAnimate actionWithAnimation:anim restoreOriginalFrame:NO];
id seq = [CCSequence actions: animAction, [CCCallFunc actionWithTarget:fixedSprite selector:@selector(removeFromParentAndCleanup:)], [CCCallFunc actionWithTarget:newSprite selector:@selector(removeFromParentAndCleanup:)], nil];
[fixedSprite runAction:seq];
}
}
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
[self removeSelectedSprite:touchLocation];
return TRUE;
}
Why is not working for sprites that are on top of each one (having different z order)?
Don't know if there is a better way but what's coming up in mind is just check the layer childrens and their z-order and remove based on z-order.