Schedule with 0 delay

Hello everyone.

I’m trying to archive a scheduled behavior not have any delay at the begining. But I mean absolutely 0 seconds delay. Let me explain what i mean.

I tried this approach,

this->schedule(CC_SCHEDULE_SELECTOR(Tower::shoot), 0.3f, CC_REPEAT_FOREVER, 0.f);

Yes this gives me 0 seconds delay at the start as I specified, but because of the 0.3 interval, scheduled behavior still delays 0.3 seconds at the first shot. Sequence goes like this; wait 0.3 seconds, fire the first bullet, then wait another 0.3 seconds, fire another bullet etc.

What I want to do is, ignore that first 0.3 delay as well. I mean when the behavior scheduled, sequence should be like this; shoot the first bullet immediately, wait 0.3 seconds, shoot second bullet, wait 0.3 seconds etc.

Can I accomplish that with built-in scheduler, or I have to implement my own scheduling mechanism at the update method.

Thanks.

Edit: I manage to provide that behavior by manually calling shoot() method for the first time just before I schedule it but it still seems a hack to me, and I’m not sure this approach will be a generic one. I’m still all ears.

I never use schedule for these kinds of delays; perhaps you’re supposed to, but I don’t know their overhead.

Instead I have an instance variable with the number of seconds until the next event (fire bullet in your case) and then manage it in my overridden update() (you’ll need to call scheduleUpdate() in your init() method to get it to fire):

YourClass.h:

class YourClass : public cocos2d::Node
{
    bool _firing;
    float _nextFireBullet;
    ...
}

YourClass.cpp:

void YourClass::update(float delta)
{
    if (_firing) {
        _nextFireBullet -= delta;
        if (_nextFireBullet <= 0.0f) {
            fireBullet();
            _nextFireBullet = 0.3f;
        }
   }
}

You then just need to manage the state of these instance variables.