admin管理员组文章数量:1567660
定时器在大部分游戏中是不可或缺的,即每隔一段时间,就要执行相应的刷新体函数,以更新游戏的画面、时间、进度、敌人的指令等等。
cocos2dx为我们提供了定时器schedule相关的操作。其操作函数的定义在CCNode中,所以基本上大多数的引擎类都可以设置定时器,如CCLayer、CCSprite、CCMenu等。
定时器更新的方式分为三类:
(1)默认定时器 :scheduleUpdate();
(2)自定义定时器:schedule();
(3)一次性定时器:scheduleOnce();
【scheduleUpdate】
默认定时器:scheduleUpdate()。
该定时器默认刷新次数与屏幕刷新频率有关。如频率为60帧每秒,那么scheduleUpdate每秒执行60次刷新。
与scheduleUpdate其对应的刷新函数体为update(),即每一帧会执行一次update()函数。
相关操作如下:
1 2 3 4 5 6 7 |
//
//开启默认定时器。刷新间隔为一帧。
void
scheduleUpdate();
void
scheduleUpdateWithPriority(
int
priority);
//给予优先级priority。priority越小,优先级越高
virtual
void
update(
float
delta);
//update为scheduleUpdate定时器的刷新函数体.
//
|
【schedule】
自定义定时器:schedule()。
该定时器可以自定义指定的刷新函数体、刷新函数体的次数、刷新频率、以及开始刷新的时间。
函数体不一定为update(),可以自己定义。
相关操作如下:
1 2 3 4 5 6 7 8 9 10 |
//
//设置自定义定时器。默认刷新间隔为一帧。
// interval : 每隔interval秒,执行一次。
// repeat : 重复次数。
// delay : 延迟时间,即创建定时器delay秒后开始执行刷新。
//schedule( schedule_selector(HelloWorld::myUpdate), 1.0/60.0 );
void
schedule(SEL_SCHEDULE selector);
//默认刷新间隔为一帧
void
schedule(SEL_SCHEDULE selector,
float
interval);
//自定义刷新间隔,单位:秒
void
schedule(SEL_SCHEDULE selector,
float
interval, unsigned
int
repeat,
float
delay);
//
|
【scheduleOnce】
一次性定时器:scheduleOnce()。
该定时器在等待delay秒延迟时间后,只执行一次刷新函数体,之后就不再刷新。
相关操作如下:
1 2 3 4 5 |
//
//只执行一次,delay秒后执行
//scheduleOnce( schedule_selector(HelloWorld::myUpdate), 5.0 );
void
scheduleOnce(SEL_SCHEDULE selector,
float
delay);
//
|
【其他操作】
定时器的取消、暂停、恢复。
相关操作如下:
1 2 3 4 5 6 7 8 9 |
//
//this->unscheduleUpdate();
//sprite->unscheduleAllSelectors();
void
unscheduleUpdate(
void
);
//取消默认定时器
void
unschedule(SEL_SCHEDULE selector);
//取消自定义函数的定时器
void
unscheduleAllSelectors(
void
);
//取消所有定时器
void
pauseSchedulerAndActions(
void
);
//暂停所有定时器和动作
void
resumeSchedulerAndActions(
void
);
//恢复所有定时器和动作
//
|
【代码实战】
1、在HelloWorld::init()中创建五个精灵
精灵和五种定义定时器的方法,一一对应。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
//
//创建五个精灵
CCSprite* sp = CCSprite::create(
"Icon.png"
);
sp->setPosition( ccp(30, mysize.height - 30) );
this
->addChild(sp, 0, 100);
//tag标记100
CCSprite* sp1 = CCSprite::create(
"Icon.png"
);
sp1->setPosition( ccp(30, mysize.height - 90) );
this
->addChild(sp1, 0, 101);
//tag标记101
CCSprite* sp2 = CCSprite::create(
"Icon.png"
);
sp2->setPosition( ccp(30, mysize.height - 150) );
this
->addChild(sp2, 0, 102);
//tag标记102
CCSprite* sp3 = CCSprite::create(
"Icon.png"
);
sp3->setPosition( ccp(30, mysize.height - 210) );
this
->addChild(sp3, 0, 103);
//tag标记103
CCSprite* sp4 = CCSprite::create(
"Icon.png"
);
sp4->setPosition( ccp(30, mysize.height - 270) );
this
->addChild(sp4, 0, 104);
//tag标记104
//定义五个定时器,更新精灵
this
->scheduleUpdate();
this
->schedule( schedule_selector(HelloWorld::myupdate) );
this
->schedule( schedule_selector(HelloWorld::myupdate2), 1.0f );
this
->schedule( schedule_selector(HelloWorld::myupdate3), 1.0f, 5, 3.0f);
this
->scheduleOnce( schedule_selector(HelloWorld::myupdate4), 5.0f );
//
|
2、编写定时器对应的刷新函数体
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
//
//scheduleUpdate
void
HelloWorld::update(
float
dt)
{
CCSprite* sp = (CCSprite*)
this
->getChildByTag(100);
//获取 tag=100 的精灵
sp->setPosition( sp->getPosition() + ccp(1,0) );
//每帧移动1
}
//schedule(schedule_selector)
void
HelloWorld::myupdate(
float
dt)
{
CCSprite* sp1 = (CCSprite*)
this
->getChildByTag(101);
//获取 tag=101 的精灵
sp1->setPosition( sp1->getPosition() + ccp(1,0) );
//每帧移动1
}
//schedule(schedule_selector, interval)
void
HelloWorld::myupdate2(
float
dt)
{
CCSprite* sp2 = (CCSprite*)
this
->getChildByTag(102);
//获取 tag=102 的精灵
sp2->setPosition( sp2->getPosition() + ccp(60,0) );
//每秒移动60
}
//schedule(schedule_selector, interval, repeat, delay)
void
HelloWorld::myupdate3(
float
dt)
{
CCSprite* sp3 = (CCSprite*)
this
->getChildByTag(103);
//获取 tag=103 的精灵
sp3->setPosition( sp3->getPosition() + ccp(60,0) );
//每秒移动60
}
//scheduleOnce
void
HelloWorld::myupdate4(
float
dt)
{
CCSprite* sp4 = (CCSprite*)
this
->getChildByTag(104);
//获取 tag=104 的精灵
sp4->setPosition( sp4->getPosition() + ccp(100,0) );
//移动100
}
//
|
3、运行结果
4、分析与总结
(1)scheduleUpdate()和schedule(schedule_selector)的效果一样,只是schedule可以自定义刷新函数体,不一定是update()。而scheduleUpdate()的刷新函数体只能为update()。
(2)schedule(schedule_selector, interval):设置了interval=1.0,所以每隔1.0秒执行了一次myupdate2()。
(3)schedule(schedule_selector, interval, repeat, delay):在开始3.0f秒后才开始执行myupdate3(),并且之后又重复执行了5次,就停止更新了。
(4)scheduleOnce(schedule_selector):在开始5秒后,只执行了一次myupdate4(),就停止更新了。
版权声明:本文标题:scheduleupdate 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/xitong/1725781590a1042257.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论