Difference between CCNode::init() and CCNode::onEnter()

Hi, i was wondering what the difference between the init() function and the onEnter() function was? Does one happen before the other?

If i run a scene it runs the init function but what happens when i change to another scene and then change back. Does it run the init function again? What about onEnter? Does it run again if i change scene and then change back?

Shane K wrote:

Hi, i was wondering what the difference between the init() function and the onEnter() function was? Does one happen before the other?
>
If i run a scene it runs the init function but what happens when i change to another scene and then change back. Does it run the init function again? What about onEnter? Does it run again if i change scene and then change back?

Init is part of creating an object and helps initialize it

Object* obj = new Object();
obj->init();

if you check the documentation onEnter() happens when the scene is presented by the CCDirector. If you are an iOS developer you can think of this like viewDidAppear. Don’t forget though, if you have a subclassed CCLayer that overrides onEnter(), you should also make sure that at the beginning of the method it calls the onEnter for CCLayer, like this:

void MyLayer::onEnter()
{
CCLayer::onEnter();
//your code goes here
}

Monocle Society wrote:

Shane K wrote:
> Hi, i was wondering what the difference between the init() function and the onEnter() function was? Does one happen before the other?
>
> If i run a scene it runs the init function but what happens when i change to another scene and then change back. Does it run the init function again? What about onEnter? Does it run again if i change scene and then change back?
>
Init is part of creating an object and helps initialize it
>
Object* obj = new Object();
obj->init();
>
if you check the documentation onEnter() happens when the scene is presented by the CCDirector. If you are an iOS developer you can think of this like viewDidAppear. Don’t forget though, if you have a subclassed CCLayer that overrides onEnter(), you should also make sure that at the beginning of the method it calls the onEnter for CCLayer, like this:
>
void MyLayer::onEnter()
{
CCLayer::onEnter();
//your code goes here
}

Can i know why CCLayer::onEnter() must be called at the beginning?

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
}

onEnter will be called after Director::popScene()?

@jinri

Pop removes the scene, so onExit() method will be called. onEnter() is called when you are entering the scene.