2011-10-12 88 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。 –