开发者

How to make a sprite disappear with various animations on its click?

I am new to Cocos2D. I am making a simple game for iPhone in which I want my sprite to disappear with some animation. Till now I can make it disappear with the following code:-

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

Now I want the sprite to disappear with so开发者_开发技巧me animation or any effect. How can I do so?


As an example, this would make your sprite shrink until it disappears then remove it from it's parent:

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

For other actions, try CCMoveTo or CCJumpTo or CCRotateBy. You can run several actions at once, so above the runAction: line I provided, try another [sprite runAction:[CCRotateBy actionWithDuration:0.4 angle:360]]

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜