Cocos2d-x中,onEnter与init的区别

这个问题我百度了下,没有找到好的答案,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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值