Cocos2d-x中,onEnter与init的区别

来源:互联网 发布:曼联92黄金一代 知乎 编辑:程序博客网 时间:2024/06/05 03:56

这个问题我百度了下,没有找到好的答案,google了一下,找到了我想要的答案,都是很简单的英文,不翻译了。

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

原链接:

https://stackoverflow.com/questions/20769747/difference-between-cclayerinit-and-cclayeronenter/20773133#20773133

Cocos2d-x has its memory allocation model as a two-step process, like objective-c. Each object has memory allocated (usually using a "create" method) and then has its state initialized (usually using a method called "init"). So in the create/init methods, you allocate the memory and do any object initialization necessary for it to run.

When the object starts to be put onto the display, or when it is added to another container, its "onEnter" method is called. This gets called when a CCScene/CCLayer (either of which may be containing your CCNode derived object) is displayed by the framework itself.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

原链接

http://discuss.cocos2d-x.org/t/difference-between-ccnode-init-and-ccnode-onenter/7878

You call onEnter() if you want to do something the moment it appears on the screen.
The init() method will be called even without the layer being on the screen.
Something like this:

void MyScene::createPopup() {     mPopup = new PopupLayer();     mPopup -> init();     mPopup -> retain();     // At this point, mPopup (which is a PopupLayer object that is a subclass of CCLayer)     // is still not on the screen, hence, PopupLayer::onEnter() is still not called}void MyScene::showPopup() {     this -> addChild( mPopupLayer, kPopupIndex, kPopupTag );     mPopup -> release();     // At this point, PopupLayer::onEnter() is called, because mPopup is being rendered}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

以下为自述:

init的调用我是知道的,就是定义在CREATE_FUNC里面的,在create函数中调用。

onEnter我查了Node类的代码,函数的定义和注释如下:

/*** 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().* @lua NA*/virtual void onEnter();
其中还特别提到了如果重载onEnter,应该在重载函数里先调用父类的函数。

Node中的addChild函数的注释中提到了onEnter:

/*** Adds a child to the container with z-order as 0.** If the child is added to a 'running' node, then 'onEnter' and 'onEnterTransitionDidFinish' will be called immediately.** @param child A child node.*/virtual void addChild(Node * child);
可以到看onEnter和onEnterTransitionDidFinish是在addChild函数里进行调用的。

也就是说只有在添加的时候才会调用onEnter,如果一个Node的子类变量只创建了,并没有添加到Scene或Layer中,那么只会调用init,不会调用onEnter.

0 0