Problem with Scheduler

Hi, I’m trying to set a function that’s not associated to any object to run every X milliseconds.

Looking at Scheduler’s doc in the API docs I thought it’d be a matter of using code like in the example that goes:

//register a schedule to scheduler
cc.director.getScheduler().scheduleSelector(callback, this, interval, !this._isRunning);

But I’ve bumped into the following problems:

  1. cc.director.getScheduler().scheduleSelector() is undefined.
  2. Assuming cc.director.getScheduler().scheduleSelector() actually worked: how can I stop this scheduled function?

Thanks to anyone who can help!

PS: what I’m really trying to accomplish is to “animate” the volume of a played music track, is there a better way to do this? Like with how we can animate a Sprite’s size with cc.Action?

PS2: I’m currently defaulting to just using setTimeOut(), but I believe it’d be best if I could use cocos’ scheduling functions, right?

Using Cocos2d-x 3.1, the correct method seems to be

cc.director.getScheduler().scheduleCallbackForTarget(callbackObj, callbackFunc, interval, repeat, delay, paused)

This worked for me to print “Hello World” every second:

cc.director.getScheduler().scheduleCallbackForTarget(this, function() { cc.log("Hello World"); }, 1);

as well as

cc.director.getScheduler().scheduleCallbackForTarget(this, this.test, 1);

when “test” is the name of a function belonging to the class which prints “Hello World”.

To unschedule/stop something appears to be

cc.director.getScheduler().unscheduleCallbackForTarget(this, this.test)

if your scheduled function is “test”.

No idea about using actions for this, but I would say using the scheduler instead of setTimeOut is probably better practice. There are some cases where it can be annoying to use setTimeOut. For example, if you want to stop all scheduled functions, you could use “unscheduleAllCallbacksForTarget” to stop all Cocos schedules, but you would probably have to manually stop all the setTimeOut schedules. And if you were using C++ code in conjunction with the JavaScript, it would be rather annoying to stop setTimeOut schedules from the C++ code.

Thanks!

The thing is, I’m not sure I can use those functions, because I have a “manager” object, that’s not a cocos2d node, from where I call this kind of functions, so this will probably not work as a parameter…

I’ll try to convert my { } “manager object” to a cc.Class and see if that does the trick.

It’d be nice if we could “animate” the music volume because that’d let us use easing functions and help us not re-invent the wheel.

Actually, I just tried creating a non-CC object - var Testing = function() { } - and tried creating a new instance - this.testing = new Testing() and used this this.testing rather than this as the callback obj for the schedule function and it worked. It looks as though any JS object can receive the callback.

And the Cocos engine is open source, so you can always make a music volume animator yourself :stuck_out_tongue:

1 Like

I have post an issue on github about this, we should support any kind of object.
https://github.com/cocos2d/cocos2d-js/issues/1147

Hi @pandamicro, that’s not really neccesary. It’s just as @grimfate says: the scheduler can be used with any kind of object!

The only real issue here is that the API docs are incorrect.

PS: it’s also kind of weird that for the scheduled function to repeat you need to pass null or cc.REPEAT_FOREVER. I tried passing true as an argument but that made it only repeat twice and then stop.

For html5 engine, as the issue indicate, we use the objects’ __instanceId to reference it in scheduler, but pure js object doesn’t have this attribute so it can’t be scheduled.

I copied the description of this method:

The scheduled method will be called every 'interval' seconds.
...
repeat let the action be repeated repeat + 1 times, use cc.REPEAT_FOREVER to let the action run continuously
delay is the amount of time the action will wait before it'll start

So when you pass true as repeat, it will be parsed to 1 and then add 1 will make it 2. That’s why it repeat twice

Yes, I’ve noticed that, but isn’t the way I say more intuitive? Shouldn’t we change this functionality?