[cocos2d-x]关于定时器

本文详细介绍了Cocos2d-x中定时器的基本概念、三种类型及其使用方法,包括默认定时器、自定义计时器和一次性定时器,并提供了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

什么是定时器

定时器的作用就是每隔一段时间,就执行一段自定义的动作,比如飞机向前方移动,子弹的移动等等。该函数定义在CCNode头文件中,基本上cocos2dx中所有的东西都能够使用定时器。

定时器的分类

第一种:scheduleupdate()默认定时器
该定时器开启函数与update()函数配套使用,update方法是每一帧执行一次,所以如果默认每秒60帧,那么每秒就执行60次。
使用方法:这里写图片描述

class Test:public Scene
{
....
virtual void update(float f);
....
}
bool init()
{
 Scene::create();
 this->scheduleupdate();//开始进行update操作
}
void update(float f)
{
....//进行的一些自定义操作
}

第二种:schedule()自定义计时器
这种定时器的灵活性更加大,比如设置新的函数代替update函数,定义刷新的次数和间隔。
这里写图片描述

    this->schedule(schedule_selector(GameTest::update),1,1,1);
     @param selector  The SEL_SCHEDULE selector to be scheduled.
     * @param interval  Tick interval in seconds. 0 means tick every frame. If interval = 0, it's recommended to use scheduleUpdate() instead.
     * @param repeat    The selector will be executed (repeat + 1) times, you can use CC_REPEAT_FOREVER for tick infinitely.
     * @param delay     The amount of time that the first tick will wait before execution.

第三种:scheduleOnce()一次性定时器
顾名思义,这种定时器只会执行一次。
这里写图片描述

this->scheduleOnce(schedule_selector(GameTest::update), 1.1);
    * @param selector      The SEL_SCHEDULE selector to be scheduled.
     * @param delay         The amount of time that the first tick will wait before execution.

定时器的通用操作

    this->unschedule(schedule_selector(GameTest::update));
    //取消自定义的函数
    this->unscheduleAllSelectors();
    //取消所有的自定义函数
    this->unscheduleUpdate();
    //取消默认的update函数
    this->unscheduleAllCallbacks();
    //取消所有的自定义函数,lambda函数,update函数,但是action动作不会被影响
    this->pauseSchedulerAndActions();
    //暂停定时器和动作
    this->resumeSchedulerAndActions();
    //恢复定时器和动作

转载于:https://www.cnblogs.com/lizhenghao126/p/11053724.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值