总结:
一、create与init方法之间的关系
Scene * Scene ::create() { Scene *ret = new (std::nothrow) Scene (); if (ret && ret->init()) { ret->autorelease(); return ret; } else { CC_SAFE_DELETE (ret); return nullptr ; } }
bool Scene ::init() { auto size = Director ::getInstance()->getWinSize(); return initWithSize(size); }
Node的init方法
bool Node ::init() { return true ; }
1、重写init()方法。
二、init与onEnter
执行顺序:init->onEnter
init在创建对象时会执行一次。
onEnter每次进入场景的时候都会被调用,cocos2d-x的注释:
* Event callback that is invoked every time when Node enters the 'stage'.
* If the Node enters the 'stage' with a transition,this event is called when the transition starts.
* During onEnter you can't access a "sister/brother" node.
* If you override onEnter,you shall call its parent's one,e.g.,Node::onEnter().
|