I think somebody already asked this before but I tried searching the forum but I can’t find the topic anymore. Where’s the best place to put a function that needs to run every frame?
The best way:
For any node clase, you can inherit from the class and call scheduleUpdate
function to register a every frame call via update
function
var myNode = cc.Node.extend({
onEnter: function() {
this._super();
this.scheduleUpdate();
},
// You must implement this function
update: function() {
// Do anything you want
}
})
The simplest way:
node.schedule(function() {
// The callback
}, 0);
The second parameter is the callback interval, when set to 0, the callback will be invoked every frame. But in this case, it’s a little more expensive than the update
function, so we suggest to use the first solution.
Our API reference: http://www.cocos2d-x.org/reference/html5-js/V3.0rc0/index.html
Thanks. I have read about the schedule and update functions, I was just wondering if there was a better way and if I remember correctly from that other thread, I think somebody mentioned something about the cc.director or something outside the node? Do you know anything about that?
It’s possible to do:
cc.director.getScheduler().scheduleCallbackForTarget(target, callback_fn, interval, repeat, delay, paused);
// or
cc.director.getScheduler().scheduleUpdateForTarget(target, priority, paused);
But they are really the same effect as the solution I told you, and it’s recommended to use the previous solution.