`

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]]];
分享到:
评论
1 楼 jxhgzs001 2011-12-05  
很好 很强大,不顶你一下 觉得过意不去啊

相关推荐

Global site tag (gtag.js) - Google Analytics