您的位置:首页 > 其它

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

2015-06-05 21:21 369 查看

动作回调函数 (例子)

bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}

Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
///////////////     动作回掉函数            /////////////////

Sprite* sprite = Sprite::create("button.png");
sprite->setPosition(Vec2(visibleSize.width * 0.2, visibleSize.height * 0.5));
this->addChild(sprite);

ActionInterval *move = MoveTo::create(3, Vec2(visibleSize.width * 0.8, visibleSize.height * 0.5));

// 空的动作回调函数
ActionInstant *func = CallFunc::create(this, callfunc_selector(HelloWorld::funcCallBack));
// 带一个参数的动作回掉函数(参数是 Node 类型的)
ActionInstant *funcN = CallFuncN::create(this, callfuncN_selector(HelloWorld::funcNCallBack));
// 带两个参数的动作回调函数(参数是 Node 类型的, 传递的数据)
ActionInstant *funcND = CCCallFuncND::create(this, callfuncND_selector(HelloWorld::funcNDCallBack),(void*) 10);

// 每隔 1s执行一个动作
sprite->runAction(Sequence::create(move, DelayTime::create(1.0f), func, DelayTime::create(3.0f), funcN, DelayTime::create(3.0f), funcND, nullptr));

///////////////     动作回掉函数            /////////////////

return true;
}

// 无参的回调函数
void HelloWorld::funcCallBack(){

log("action end");
}

// 获取精灵的回调函数;精灵变大 3 倍
void HelloWorld::funcNCallBack(Node *pSender){

Sprite* sprite = (Sprite*)pSender;
ActionInterval *scale = ScaleTo::create(1.0f, 3.0f);
sprite->runAction(scale);
}

// 获取精灵和数据的回调函数:精灵移动到(200,200)的位置,并输出 data 的值
void HelloWorld::funcNDCallBack(cocos2d::Node *pSender, void* data){

Sprite* sprite = (Sprite*)pSender;
ActionInterval *move = MoveTo::create(1.0f, Vec2(200,200));
sprite->runAction(move);

log("data %d", data);
}


在精灵运动到指定位置后

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

3s后精灵变大3倍

3s后精灵移动到(200,200)位置处并输出 data 10
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: