动作回调函数 (CallFunc,CallFuncN,CCCallFuncND)

动作回调函数 (例子)

 1 bool HelloWorld::init()
 2 {
 3     //////////////////////////////
 4     // 1. super init first
 5     if ( !Layer::init() )
 6     {
 7         return false;
 8     }
 9 
10     Size visibleSize = Director::getInstance()->getVisibleSize();
11     Vec2 origin = Director::getInstance()->getVisibleOrigin();
12 ///////////////     动作回掉函数            /////////////////
13 
14     Sprite* sprite = Sprite::create("button.png");
15     sprite->setPosition(Vec2(visibleSize.width * 0.2, visibleSize.height * 0.5));
16     this->addChild(sprite);
17 
18     ActionInterval *move = MoveTo::create(3, Vec2(visibleSize.width * 0.8, visibleSize.height * 0.5));
19     
20     // 空的动作回调函数
21     ActionInstant *func = CallFunc::create(this, callfunc_selector(HelloWorld::funcCallBack));    
22     // 带一个参数的动作回掉函数(参数是 Node 类型的)
23     ActionInstant *funcN = CallFuncN::create(this, callfuncN_selector(HelloWorld::funcNCallBack));    
24     // 带两个参数的动作回调函数(参数是 Node 类型的, 传递的数据)
25     ActionInstant *funcND = CCCallFuncND::create(this, callfuncND_selector(HelloWorld::funcNDCallBack),(void*) 10);    
26 
27     // 每隔 1s执行一个动作
28     sprite->runAction(Sequence::create(move, DelayTime::create(1.0f), func, DelayTime::create(3.0f), funcN, DelayTime::create(3.0f), funcND, nullptr));
29 
30 
31     ///////////////     动作回掉函数            /////////////////
32 
33 
34     
35     return true;
36 }
37 
38 // 无参的回调函数
39 void HelloWorld::funcCallBack(){
40 
41     log("action end");
42 }
43 
44 // 获取精灵的回调函数;精灵变大 3 倍
45 void HelloWorld::funcNCallBack(Node *pSender){
46 
47     Sprite* sprite = (Sprite*)pSender;
48     ActionInterval *scale = ScaleTo::create(1.0f, 3.0f);
49     sprite->runAction(scale);
50 }
51 
52 // 获取精灵和数据的回调函数:精灵移动到(200,200)的位置,并输出 data 的值
53 void HelloWorld::funcNDCallBack(cocos2d::Node *pSender, void* data){
54 
55     Sprite* sprite = (Sprite*)pSender;
56     ActionInterval *move = MoveTo::create(1.0f, Vec2(200,200));
57     sprite->runAction(move);
58 
59     log("data %d", data);
60 }

在精灵运动到指定位置后

1s后控制台才输出  action end 语句

3s后精灵变大3倍

3s后精灵移动到(200,200)位置处并输出  data  10

原文地址:https://www.cnblogs.com/dudu580231/p/4555607.html