精灵三秒消失 cocos2d

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]]];  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值