var HelloWorldScene = cc.Scene.extend({
onEnter:function () {
console.log("js onEnter start");
this._super();
console.log("js onEnter end");
}
});
这样一个scene,c++中Scene的onEnter会执行两次, js中scene和c++中的Node的onEnter正常执行一次。
这个问题和js继承c++ class的机制有关,触发顺序:
C++:
runScene -> Scene::onEnter -> Node::onEnter
Node判断自己有js绑定,调用js的onEnter并跳过自己的onEnter逻辑
js:
this.super_() -> js binding onEnter() -> setCalledFromScript(true) -> 再次调用c++ onEnter
回到c++:
Scene::onEnter (注意这里是第二次重复的地方,由于多态实际因此并不是直接调用Node::onEnter ) -> Node::onEnter 重置setCalledFromScript并执行刚才跳过的onEnter逻辑
理论上,当使用js 重写c++ class的onEnter等函数时,c++继承链中所有Node以外的class的onEnter,onExit等都执行两次