用动画,不同的z顺序删除精灵

时间:2012-01-26 07:20:57

标签: objective-c cocos2d-iphone ccsprite

如何删除每个精灵上面但具有不同z顺序的精灵?

我正在使用的代码是:

- (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;    
}

为什么不为每个精灵(具有不同的z顺序)的精灵工作?

1 个答案:

答案 0 :(得分:0)

不知道是否有更好的方法,但是想到的只是检查图层子项及其z顺序并根据z顺序删除。