精灵三秒消失 cocos2d

来源:互联网 发布:mac 照片 编辑:程序博客网 时间:2024/04/30 06:07
1.问题描述: 如果我想让一个 sprite 显示3秒钟然后消失, 

使用 CCDelayTime 和 CCCallFunc 

Oc代码  收藏代码
  1. CCSprite *sprite = [CCSprite spriteWithFile:@"blabla.png"];  
  2. [layer addChild:sprite];  
  3. CCDelayTime* waitAction = [CCDelayTime actionWithDuration:3]; //等待3秒  
  4. CCCallFunc* vanishAction = [CCCallFunc actionWithTarget:self selector:@selector(removeSprite:)]; //调用removeSprite:方法  
  5. CCSequence* sequence = [CCSequence actions:waitAction, vanishAction, nil];  
  6. [sprite runAction:sequence];  
  7.    
  8. // 在 removeSprite: 里  
  9. [sprite removeFromParentAndCleanup:YES];  


2.0.9.6以后的动作 

由于0.9.9.5以后没有spritesheet 了,但是之前的教程却都用这个方法,找了老半天,终于知道新版本的动画效果制作了: 
Oc代码  收藏代码
  1. CCSpriteBatchNode * spritesheet = [CCSpriteBatchNode batchNodeWithFile:@"bee.png"];  
  2.         [self addChild:spritesheet];  
  3.           
  4.         for (int i = 0; i < 2; i++) {  
  5.             CCSpriteFrame* frame = [[CCSpriteFrame alloc] initWithTexture:spritesheet.texture rect:CGRectMake(i*3803738)];  
  6.             [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFrame:frame name:[NSString stringWithFormat:@"playerFrame%d", i]];  
  7.             [frame release];  
  8.         }  
  9.           
  10.         SPBee = [[CCSprite alloc] initWithSpriteFrameName:[NSString stringWithFormat:@"playerFrame%d"0]];  
  11.         [spritesheet addChild:SPBee];  
  12.         [SPBee release];  
  13.         [SPBee setPosition:CGPointMake(260, winSize.height-305)];  
  14.           
  15.         NSMutableArray* animFrames = [NSMutableArray array];  
  16.         for (int i = 0; i < 2; i++) {  
  17.             CCSpriteFrame* frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"playerFrame%d", i]];  
  18.             [animFrames addObject:frame];  
  19.         }  
  20.         CCAnimation *animation = [CCAnimation animationWithFrames:animFrames delay:0.2f];  
  21.         [SPBee runAction:[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animation restoreOriginalFrame:NO]]];