特殊动作:
CallFuncCCCallFunc动作允许你从一个动作中调用一个方法。在连续动作的结尾这个方法很有用。举例:id actionTo = [CCMoveTo actionWithDuration: 2 position: ccp(s.width-40, s.height-40)];id actionBy = [CCMoveBy actionWithDuration: 2 position: ccp(80,80)];id actionCallFunc = [CCCallFunc actionWithTarget:self selector:@selector(doATask)];id actionSequence = [CCSequence actions: actionTo, actionBy, actionCallFunc, nil];-(void) doATask{ //some code}CallFuncN和CallFuncND有两种CCCallFunc,它们是CCCallFuncN和CCCallFuncNDCCCallFuncN用一个结点做实参,CCCallFuncND用结点和指针来传递数据:id actionCallFuncN = [CCCallFuncN actionWithTarget:self selector:@selector(doATaskN:)];// make sure you notice the trailing :
id actionCallFuncND = [CCCallFuncND actionWithTarget:self selector:@selector(doATaskND:data:) data:pointerToSomeData];
- (void) doATaskN: (id)node
{
//some code
}
- (void) doATaskND: (id)node data:(void*)d
{
//some code
}
CallBlock / CallBlockN
// BCA is cocos2d macro that means: Block Copy Autorelease
// CCCallBlock (no arguments)
id blockAction = [CCCallBlock actionWithBlock:^{
[label setString:@"Called Block!"];
}];
// CCCallBlockN (passes the Node as an argument)
void (^block)(CCNode*) = BCA(^(CCNode *n) {
// do something generic with node
CCLOG(@"called block for %@", n);
});
id blockAction2 = [CCCallBlockN actionWithBlock:block];
CCFollow模拟数码相机,接下来的顶点会居中:
// create an spriteid sprite = [CCSprite xxxx]; [self addChild:sprite];
[self runAction: [CCFollow actionWithTarget:sprite worldBoundary:CGRectMake(0, 0, (winSize.width*2)-100, winSize.height)]];
CCPropertyAction这是一个让你修改任意的object-c类的属性的普通动作// It will modify the "rotation" property from 0 to -270 in 2 seconds, and then it will run the reverse action.id rot =[CCPropertyAction actionWithDuration:2key:@"rotation" from:0 to:-270];
id rot_back = [rot reverse];
id rot_seq =[CCSequence actions:rot, rot_back, nil];
附练习代码(CCFuncBlock的2种形式):
//移动到指定位置 CCMoveTo *moveTo = [CCMoveTo actionWithDuration:1 position: ccp(convertedLocation.x,convertedLocation.y)]; //用CCCallBlock的block方式// CCCallBlock *block1 = [CCCallBlock actionWithBlock:^(void){// [flight runAction:[CCRotateBy actionWithDuration:2 angle:45]];// [self flightBezier:convertedLocation];// }]; void (^blockTemp1)(void) = ^(void){ [flight runAction:[CCRotateBy actionWithDuration:2 angle:45]]; [self flightBezier:convertedLocation]; }; CCCallBlock *block1 = [CCCallBlock actionWithBlock:blockTemp1]; //用CCCallBlockN的block方式 带参// CCCallBlockN *block2 = [CCCallBlockN actionWithBlock:^(CCNode *node) {// [(CCSprite *)node runAction:[CCRotateBy actionWithDuration:2 angle:45]];// [self flightBezier:convertedLocation];// }]; void (^blockTemp2)(CCNode*) = ^(CCNode *node) { // do something generic with node [(CCSprite *)node runAction:[CCRotateBy actionWithDuration:2 angle:45]]; [self flightBezier:convertedLocation]; }; CCCallBlockN *block2 = [CCCallBlockN actionWithBlock:blockTemp2]; id seq = [CCSequence actions: moveTo, block1, nil]; [seq setTag:105]; [flight runAction:seq];