cocos2d-x CCCallFuncN中node CCCallFuncND中data

来源:互联网 发布:sql server 分组查询 编辑:程序博客网 时间:2024/05/17 04:50

CCCallFuncN:

复制代码
复制代码
 1    CCCallFuncN *instant = new CCCallFuncN;
2 instant->initWithTarget(this, callfuncN_selector(ActionCallFunc::callback2));
3 m_grossini->runAction(instant);
4
5 void ActionCallFunc::callback2(CCNode* pSender)
6 {
7 CCSize s = CCDirector::sharedDirector()->getWinSize();
8 label->setPosition(CCPointMake( s.width/4*2,s.height/2));
9 pSender->setPosition( ccp(0, 0) );
10 }
复制代码
复制代码

node即指代runAction的node节点;这样当触发callback2的时候即可通过pSender来改变m_grossini的一些状态。

  CCCalFuncND:

复制代码
复制代码
 1    CCCallFuncND *instant2 = new CCCallFuncND;
2 instant2->initWithTarget(this, callfuncND_selector(ActionCallFunc::callback3), (void*)"fgd");
3
4 void ActionCallFunc::callback3(CCNode* pTarget, void* data)
5 {
6 CCSize s = CCDirector::sharedDirector()->getWinSize();
7 CCLabelTTF *label = CCLabelTTF::labelWithString("fgd", "Marker Felt", 16);
8 label->setPosition(CCPointMake( s.width/4*3,s.height/2));
9 addChild(label);
10 }
复制代码
复制代码

Data可以将数据指针传进去,不过类型丢了必须强转。
virtual bool initWithTarget (SelectorProtocol *pSelectorTarget, SEL_CallFuncND selector, void *d);现在对SelectorProtocol不太明白,为什么不是node,要是node的话我可以理解成父节点(psender的父节点),但是为啥是SelectorProtocol呢?

  SelectorProtocol:
  CCNode是SelectorProtocol的子类

 

依次类推 

CCCallFunc 

不带node,也不带数据

CCCallFunc* func = new CCCallFunc;

func->initWithTarget(this,callfunc_selector(HSGamePairLayer::Shake()));

或者

  1. CCFiniteTimeAction* actions= CCSequence::actions(action,  
  2.                                    CCCallFunc::actionWithTarget(this,callfunc_selector(MyClass::callBackAnim)),NULL);  
CCCallFuncO,回调,O表示CCObject,CCCallFunco可以带CCObject参数的函数
 
[cpp]  
CCSequence::actions(CCCallFuncO::actionWithTarget(this, callfuncO_selector(ExtendActionLayer::onCallBack2), CCInteger::create(2));  
  
void ExtendActionLayer::onCallBack2(CCObject* data)  
{  
    CCInteger* num = dynamic_cast<CCInteger*>(data);  
    pSender->runAction(CCTintBy::actionWithDuration(num->getValue(), 255, 0, 255));  
}  
 
 

在游戏设计时,我们需要不断的改变屏幕显示来反映游戏操作的效果,最简单的就是提示用户已经进行的游戏时间。为此,我们需要使用cocos2d-x内置的任务调度机制,即CCNode的schedule成员函数。 

 

void schedule (SEL_SCHEDULE selector) schedules a selector. 
void schedule (SEL_SCHEDULE selector, ccTime interval) schedules a custom selector with an interval time in seconds. 
void unschedule (SEL_SCHEDULE selector) unschedules a custom selector. 
void unscheduleAllSelectors (void) unschedule all scheduled selectors: custom selectors, and the 'update' selector. cocos2d-x中的schedule有两种作用:

1)定时执行方法
 例如,每隔1秒就执行GameLayer类的方法step(ccTime dt)。

 

 

this->schedule(schedule_selector(GameLayer::step), 1.0f);
...
void GameLayer::step(ccTime dt)
{
...
}
2)延时执行方法
 例如, 延时 5秒执行GameLayer类的方法step(ccTime dt)。

 

 

this->schedule(schedule_selector(GameLayer::step), 5.0f);
...
void GameLayer::step(ccTime dt)
{
this->unschedule(schedule_selector( GameLayer::step ));
...
}
0 0
原创粉丝点击