2011-10-12 61 views
3

我是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

举个例子,这将让你的精灵缩小,直至消失,然后删除它从它的父:

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

+0

感谢James,您的建议很有帮助。具体来说,我正在构建一个应用程序,在触摸时气球爆炸。我想要一种爆炸效果或者看起来更合适的东西。 –

+1

您可能想要使用一系列帧来实现爆炸动画。查看[CCAnimation](http://www.cocos2d-iphone.org/api-ref/0.99.5/interface_c_c_animation.html)的编码和[Zwoptex](http://zwoptexapp.com/)创建spritesheet。 –