精靈三秒消失 cocos2d
1.問題描述: 如果我想讓一個 sprite 顯示3秒鍾然後消失,使用 CCDelayTime 和 CCCallFunc
- CCSprite *sprite = [CCSprite spriteWithFile:@"blabla.png"];
- [layer addChild:sprite];
- CCDelayTime* waitAction = [CCDelayTime actionWithDuration:3]; //等待3秒
- CCCallFunc* vanishAction = [CCCallFunc actionWithTarget:self selector:@selector(removeSprite:)]; //調用removeSprite:方法
- CCSequence* sequence = [CCSequence actions:waitAction, vanishAction, nil];
- [sprite runAction:sequence];
- // 在 removeSprite: 裏
- [sprite removeFromParentAndCleanup:YES];
2.0.9.6以後的動作
由於0.9.9.5以後沒有spritesheet 了,但是之前的教程卻都用這個方法,找了老半天,終於知道新版本的動畫效果製作了:
- CCSpriteBatchNode * spritesheet = [CCSpriteBatchNode batchNodeWithFile:@"bee.png"];
- [self addChild:spritesheet];
- for (int i = 0; i < 2; i++) {
- CCSpriteFrame* frame = [[CCSpriteFrame alloc] initWithTexture:spritesheet.texture rect:CGRectMake(i*38, 0, 37, 38)];
- [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFrame:frame name:[NSString stringWithFormat:@"playerFrame%d", i]];
- [frame release];
- }
- SPBee = [[CCSprite alloc] initWithSpriteFrameName:[NSString stringWithFormat:@"playerFrame%d", 0]];
- [spritesheet addChild:SPBee];
- [SPBee release];
- [SPBee setPosition:CGPointMake(260, winSize.height-305)];
- NSMutableArray* animFrames = [NSMutableArray array];
- for (int i = 0; i < 2; i++) {
- CCSpriteFrame* frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"playerFrame%d", i]];
- [animFrames addObject:frame];
- }
- CCAnimation *animation = [CCAnimation animationWithFrames:animFrames delay:0.2f];
- [SPBee runAction:[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animation restoreOriginalFrame:NO]]];
最後更新:2017-04-02 22:16:35