Is there something like on scene loaded?

I have a persistent node that is used for all scenes in my project.

I would like this persistent node to run functions each time a new scene is loaded.
How to detect a new scene is loaded ? I searched around couldn’t find something like a on scene loaded listener.

The solution I have is for each of the scene, use a script to find this persistent node and when this node is found, run the functions from persistent node. Is there a function that the engine provides for this?

1 Like

cocos2d::Node::setOnEnterCallback should fit your need.

1 Like

I cannot find this function in the docs for Cocos Creator Node api reference.
http://www.cocos2d-x.org/docs/creator/api/en/classes/Node.html?h=node

How about this

http://www.cocos2d-x.org/docs/creator/api/en/classes/Director.html#on

1 Like

No setOnEnterCallback method in Director.

Assuming what you mean is to run the persistent node after the new scene is loaded, you can try EVENT_AFTER_SCENE_LAUNCH event.

        cc.director.once(cc.Director.EVENT_AFTER_SCENE_LAUNCH, function () {
            ...
        });
1 Like

It works for each scene load, I had placed it in start function. Instead of ‘once’, use ‘on’.

cc.director.on(cc.Director.EVENT_AFTER_SCENE_LAUNCH, function () {
            ...
        });

Just an additional info regarding to ‘on’, If you use it right before every scene change, just make sure you only call that function once, or turn it off right after use.

If you bind this every time you change to a new scene, it will newly register the callback, which adds up more functions to trigger by that event. This means previous callback will be called again and most often provide duplicated/undesired result and also a memory leak.

1 Like