博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[泰然翻译] cocos2d programming guide 特殊动作 (CallFunc、CCPropertyAction)
阅读量:6345 次
发布时间:2019-06-22

本文共 3191 字,大约阅读时间需要 10 分钟。

特殊动作:

CallFunc
CCCallFunc动作允许你从一个动作中调用一个方法。在连续动作的结尾这个方法很有用。
举例:
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和CCCallFuncND
CCCallFuncN用一个结点做实参,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];

 

 

 

 

转载于:https://www.cnblogs.com/pengyingh/articles/2440775.html

你可能感兴趣的文章
web前端笔记
查看>>
import 路径
查看>>
使用optimizely做A/B测试
查看>>
finally知识讲解
查看>>
Matplotlib绘图与可视化
查看>>
openstack ocata版(脚本)控制节点安装
查看>>
【微信公众号开发】获取并保存access_token、jsapi_ticket票据(可用于微信分享、语音识别等等)...
查看>>
在开发中处理海量数据的方法 思路
查看>>
datatable 获取最大值
查看>>
sqlserver2012一直显示正在还原(Restoring)和从单用户转换成多用户模式(单用户连接中)...
查看>>
spark复习总结02
查看>>
李瑞红201771010111《第九周学习总结》
查看>>
[译]ZOOKEEPER RECIPES-Barriers
查看>>
navicat下载安装和激活一分钟完成
查看>>
6_5 一些有用网址
查看>>
NFC 鏈表操作
查看>>
pymongo模块
查看>>
第0次作业
查看>>
思维导图五个关键秘诀
查看>>
Ubuntu里设置python默认版本为python3(转载)
查看>>