如何使精灵在点击时以各种动画消失?

时间:2011-10-12 08:00:56

标签: iphone objective-c ios cocos2d-iphone

我是Cocos2D的新手。我正在为iPhone做一个简单的游戏,我希望我的精灵可以通过一些动画消失。到现在为止,我可以使用以下代码使其消失: -

-(void)selectSpriteForTouch:(CGPoint)touchLocation
{ 

    for (CCSprite *sprite in targets)  
    {

        if (CGRectContainsPoint(sprite.boundingBox, touchLocation)) 
        {
            NSLog(@"sprite was touched");

            [sprite.parent removeChild:sprite cleanup:YES];
            [[SimpleAudioEngine sharedEngine] playEffect:@"pop.wav"];
            [[SimpleAudioEngine sharedEngine] setEffectsVolume:4.0f];

        }
    }
}

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

    for( UITouch *touch in touches )
    {

        CGPoint location = [touch locationInView: [touch view]];

        location = [[CCDirector sharedDirector] convertToGL: location];

        [self selectSpriteForTouch:location];
        NSLog(@"touch was detected");
    }   
}

现在我希望精灵可以通过一些动画或任何效果消失。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

作为一个例子,这会使你的精灵缩小,直到它消失,然后将它从它的父亲中删除:

-(void)selectSpriteForTouch:(CGPoint)touchLocation
    ...
    if (CGRectContainsPoint(sprite.boundingBox, touchLocation))
    {
        [sprite runAction:[CCSequence actions:
            [CCScaleTo actionWithDuration:0.4 scale:0],
            [CCCallFuncO actionWithTarget:self selector:@selector(removeSprite:) object:sprite],
            nil]];
            ...//play audio etc
    }
    ....
}

-(void) removeSprite:(CCSprite*) s
{
    [s.parent removeChild:s cleanup:YES];
}

对于其他操作,请尝试CCMoveToCCJumpToCCRotateBy。您可以一次运行多个操作,因此在我提供的runAction:行之上,尝试另一个[sprite runAction:[CCRotateBy actionWithDuration:0.4 angle:360]]