当前位置:编程学习 > wap >>

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 = [CCSpriteFrameCache sharedSpriteFrameCache];
   
    // 1.缓冲sprite帧和纹理
    NSString *spriteFrameCacheName = [NSString stringWithFormat:@"%@.plist", animName];
    [frameCache addSpriteFramesWithFile:spriteFrameCacheName];
   
    // 2.创建一个精灵批处理结点
    NSString *spriteSheetName = [NSString stringWithFormat:@"%@.png", animName];
    CCSpriteBatchNode *spriteBatchNode = [CCSpriteBatchNode batchNodeWithFile:spriteSheetName];
    [parent addChild:spriteBatchNode z:2];
   
    // 3.收集帧列表
    NSMutableArray *frames = [NSMutableArray array];
    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 = [CCAnimate actionWithAnimation: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];
    [spriteBatchNode addChild:animSprite];
    return animSprite;
}
- (void) removeSprite:(id)sender batchNode:(CCSpriteBatchNode*)batchNode {    // 由 CCCallFuncND 调用~
    CCSprite *animSprite = (CCSprite*)sender;
    [batchNode removeChild:animSprite cleanup:YES];
}


摘自 yang3wei的专栏
补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,