转自:http://blog.csdn.net/kyfvc/article/details/8268217
在游戏中,要检测两个运动的物体什么时候否碰撞,就必须在每一帧中对两个物体的矩阵范围进行判断,是否出现相交。同样的,一些游戏动画,为了做出动画效果,我们在设置动作的同时,还需要对每一帧的画面进行布局,在cocos2d-x中就可以使用schedule()函数实现了。
下面是schedule()函数的定义:
- void CCNode::schedule(SEL_SCHEDULE selector)
- {
- this->schedule(selector, 0);
- }
- void CCNode::schedule(SEL_SCHEDULE selector, float interval)
- {
- CCAssert( selector, "Argument must be non-nil");
- CCAssert( interval >=0, "Argument must be positive");
- CCScheduler::sharedScheduler()->scheduleSelector(selector, this, interval, !m_bIsRunning);
- }
- void CCNode::schedule(SEL_SCHEDULE selector)
- {
- this->schedule(selector, 0);
- }
- void CCNode::schedule(SEL_SCHEDULE selector, float interval)
- {
- CCAssert( selector, "Argument must be non-nil");
- CCAssert( interval >=0, "Argument must be positive");
- CCScheduler::sharedScheduler()->scheduleSelector(selector, this, interval, !m_bIsRunning);
- }
- void <font style=""><font style=""><span style=""><span style=""><span style="">C</span><span style="">C</span></span></span></font></font>Node::s<span style="">c</span>hedule(SEL_S<span style="">C</span>HEDULE sele<span style="">c</span>tor)
- {
- this->s<span style="">c</span>hedule(sele<span style="">c</span>tor, 0);
- }
- void <font style=""><font style=""><span style=""><span style=""><span style="">C</span><span style="">C</span></span></span></font></font>Node::s<span style="">c</span>hedule(SEL_S<span style="">C</span>HEDULE sele<span style="">c</span>tor, float interval)
- {
- <font style=""><font style=""><span style=""><span style=""><span style="">C</span><span style="">C</span></span></span></font></font>Assert( sele<span style="">c</span>tor, "Argument must be non-nil");
- <font style=""><font style=""><span style=""><span style=""><span style="">C</span><span style="">C</span></span></span></font></font>Assert( interval >=0, "Argument must be positive");
- <font style=""><font style=""><span style=""><span style=""><span style="">C</span><span style="">C</span></span></span></font></font>S<span style="">c</span>heduler::sharedS<span style="">c</span>heduler()->s<span style="">c</span>heduleSele<span style="">c</span>tor(sele<span style="">c</span>tor, this, interval, !m_bIsRunning);
- }
这里我只说第二个函数,第一个参数是回调类型,这里使用schedule_selector,如schedule_selector(MyClass::MyCallbackFunction),第二个参数是间隔时间,即多久调用MyCallbackFunction函数;
step1:创建cocos2d-win32项目,并命名为scheduler;
step2:在HelloWorldScene.h中添加如下类:
- class ScheduleTest:public CCLayer
- {
- protected:
- int posX;//用于每次调用addTarget函数时增加对象水平方向的位置
- public:
- ScheduleTest();
- virtual void onEnter();
- void addTarget(float dt);//每次调用会增加一个精灵
- };
- class ScheduleTest:public CCLayer
- {
- protected:
- int posX;//用于每次调用addTarget函数时增加对象水平方向的位置
- public:
- ScheduleTest();
- virtual void onEnter();
- void addTarget(float dt);//每次调用会增加一个精灵
- };
- class ScheduleTest:public <span style="">CC</span>Layer
- {
- protected:
- int posX;//用于每次调用addTarget函数时增加对象水平方向的位置
- public:
- ScheduleTest();
- virtual void onEnter();
- void addTarget(float dt);//每次调用会增加一个精灵
- };
step3:在HelloWorldScene.cpp中添加如下函数:
- ScheduleTest::ScheduleTest()
- {
- posX = 0;
- }
- void ScheduleTest::onEnter()
- {
- CCLayer::onEnter();
- schedule(schedule_selector(ScheduleTest::addTarget), 1);
- }
- void ScheduleTest::addTarget(float dt)
- {
- posX += 10;
- CCSprite* player = CCSprite::spriteWithFile("player.png");
- addChild(player);
- player->setPosition(ccp(posX, 100));
- }
- ScheduleTest::ScheduleTest()
- {
- posX = 0;
- }
- void ScheduleTest::onEnter()
- {
- CCLayer::onEnter();
- schedule(schedule_selector(ScheduleTest::addTarget), 1);
- }
- void ScheduleTest::addTarget(float dt)
- {
- posX += 10;
- CCSprite* player = CCSprite::spriteWithFile("player.png");
- addChild(player);
- player->setPosition(ccp(posX, 100));
- }
- ScheduleTest::ScheduleTest()
- {
- posX = 0;
- }
- void ScheduleTest::onEnter()
- {
- <span style="">CC</span>Layer::onEnter();
- schedule(schedule_selector(ScheduleTest::addTarget), 1);
- }
- void ScheduleTest::addTarget(float dt)
- {
- posX += 10;
- <span style="">CC</span>Sprite* player = <span style="">CC</span>Sprite::spriteWithFile("player.png");
- addChild(player);
- player->setPosition(<span style="">cc</span>p(posX, 100));
- }
同时修改HelloWorldScene中的scene函数,将HelloWorld *layer = HelloWorld::node();改为
ScheduleTest* layer = new ScheduleTest();
step4:编译运行程序,可以看到每隔一秒钟,背景就会增加一个人物;
源代码下载地址:http://download.csdn.net/download/wen294299195/4525824