现在的位置: 首页 > 综合 > 正文

cocos2d sprite执行完动作之后调用回调自行销毁

2018年05月18日 ⁄ 综合 ⁄ 共 1567字 ⁄ 字号 评论关闭

转载自:http://www.devdiv.com/home.php?mod=space&uid=23234&do=blog&id=3317

原文标题:

函数调用 cccallfunc

之前曾遇到一个关于传值的问题,刚好今早遇到,把相关的都贴进来,知易教程上的内容


1,函数
在动作序列中间或者结束调用某个函数,执行任何需要执行的任务:动作、状态修改等。代码如下:
- (void) OnCallFunc:(id) sender {
id ac1 = [CCMoveBy actionWithDuration:2 position:ccp(200, 200)];
id ac2 = [ac1 reverse];
id acf = [CCCallFunc actionWithTarget:self selector:@selector(CallBack1)];
[sprite runAction:[CCSequence actions:ac1, acf, ac2, nil]];
}
对应的函数为:(再做一个动作,这就实现了动作、动作序列癿任意扩展和连接)

- (void) CallBack1 {
[sprite runAction:[CCTintBy actionWithDuration:0.5 red:255 green:0 blue:255]]; 
}

2,带对象参数 调用自定义函数时,传递当前对象。
- (void) OnCallFuncN:(id) sender {
id ac1 = [CCMoveBy actionWithDuration:2 position:ccp(200, 200)];
id ac2 = [ac1 reverse];
id acf = [CallFuncN actionWithTarget:self selector:@selector(CallBack2:)];
[sprite runAction:[CCSequence actions:ac1, acf, ac2, nil]];
}
对应的自定义函数:(这里,我们直接使用了该对象)
- (void) CallBack2:(id)sender {
[sender runAction:[CCTintBy actionWithDuration:1 red:255 green:0 blue:255]]; }

3,带对象、数据参数 调用自定义函数时,传递当前对象和一个常量(也可以是指针)。
- (void) OnCallFuncND:(id) sender {
id ac1 = [CCMoveBy actionWithDuration:2 position:ccp(200, 200)];
id ac2 = [ac1 reverse];
id acf = [CCCallFuncND actionWithTarget:self selector:@selector(CallBack3:data:) data:(void*)2];
[sprite runAction:[CCSequence actions:ac1, acf, ac2, nil]];
}
对应的自定义函数,我们使用了传递的对象和数据:
-(void) CallBack3:(id)sender data:(void*)data {
[sender runAction:[CCTintBy actionWithDuration:(NSInteger)data red:255 green:0 blue:255]]; }

着重注意 CCCallFuncN 与 CCCallFunc 的区别:

前者的回调函数签名为(欲执行完一些列动作之后自动将sprite从母层中移除,就得用这个!!):

- (void) CallBack2:(id)sender 

sender 表示执行动作序列的 CCSprite 对象~

而后者的回调函数签名为:

- (void) CallBack1 

是不带参数的!!经我试验,CCCallFunc也可以采用带参数的回调,虽然不报错,但也没法发挥作用~

抱歉!评论已关闭.