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]]];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值