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];

}


你可能感兴趣的:(call)