CCCallFunc, CCCallFuncN, CCCallFuncND 三者的区别

1.CCCallFunc 只能调用不带参数的回调方法

2.CCCallFuncN 可以带一个 参数:

(id)sender

3.CCCallFuncND 可以带两个参数 (第二个参数的类型可以是任意类型):

(id)sender batchNode:(CCSpriteBatchNode*)batchNode


直接上用例(sprite动画一次性播放以后将自己从父节点移除并销毁):

- (CCSprite*) addOneOffAnimSprite:(CCNode*)parent

position:(CGPoint)position

animName:(NSString*)animName

startIndex:(int)startIndex

endIndex:(int)endIndex

delay:(float)delay

{

CCSpriteFrameCache *frameCache = [CCSpriteFrameCachesharedSpriteFrameCache];

// 1.缓冲sprite帧和纹理

NSString *spriteFrameCacheName = [NSStringstringWithFormat:@"%@.plist", animName];

[frameCache addSpriteFramesWithFile:spriteFrameCacheName];

// 2.创建一个精灵批处理结点

NSString *spriteSheetName = [NSString stringWithFormat:@"%@.png", animName];

CCSpriteBatchNode *spriteBatchNode = [CCSpriteBatchNodebatchNodeWithFile:spriteSheetName];

[parent addChild:spriteBatchNode z:2];

// 3.收集帧列表

NSMutableArray *frames = [NSMutableArrayarray];

for(int i = startIndex; i < endIndex+1; ++ i) {

NSString *frameName = [NSString stringWithFormat:@"%@%d.png", animName, i];

CCSpriteFrame *spriteFrame = [frameCache spriteFrameByName: frameName];

[frames addObject: spriteFrame];

}

// 4.创建动画对象

CCAnimation *animation = [CCAnimation animationWithFrames:frames delay:delay];

id action = [CCAnimateactionWithAnimation:animation restoreOriginalFrame:NO];

id callFunc = [CCCallFuncND actionWithTarget:self selector:@selector(removeSprite:batchNode:)data:spriteBatchNode];

id animSequence = [CCSequence actions:action, callFunc, nil];

// 5.创建 sprite 并且让它 run 动画 action~

NSString *fristFrameName = [NSString stringWithFormat:@"%@%d.png", animName, startIndex];

CCSprite *animSprite = [CCSprite spriteWithSpriteFrameName:fristFrameName];

[animSprite setPosition:position];

[animSprite runAction:animSequence];

[spriteBatchNodeaddChild:animSprite];

return animSprite;

}

- (void) removeSprite:(id)sender batchNode:(CCSpriteBatchNode*)batchNode { // CCCallFuncND调用~

CCSprite *animSprite = (CCSprite*)sender;

[batchNode removeChild:animSprite cleanup:YES];

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值