How to call something when the application resume

I want to change something in the code when the application resume. Ex.

mySprite>setVisible(true);

Where I can do it? Maybe in the onEnter() method or what?

Hello

You can overload virtual void applicationWillEnterForeground(); in class inherited from cocos2d::Application.

Hey I already know it! It is in the AppDelegate class. But, How to call something in my Layer class from it? I have tried to call something from that function but I got problems. Is there a method that I call directly in my Layer class? Or what is the right way to call something from applicationDidFinishLaunching()method?

Sandro Italiano wrote:

I have tried to call something from that function but I got problems.

What problems exactly? Please provide us with some code of yours.

Ok! In the AppDelegate.ccp I call the doSomething() static method of the HelloWorld class…

void AppDelegate::applicationWillEnterForeground() 
{
    CCDirector::sharedDirector()->startAnimation();
    HelloWorld::doSomething();
}

And then in the doSomething() method I set the visibility of a CCSprite object “blueSquare”.

void HelloWorld::doSomething()
{
    blueSquare->setVisible(false);
}

So, compiling the project I get this error: invalid use of member in ‘HelloWorld::blueSquare’ in static member function. Basically I can’t use blueSquare in that function.

I am sorry, but that’s not an engine problem. Sure you can’t work with class members in static functions (btw, there is no need to do that). That’s C++ basics.

For example, you can save scene pointer as a member of AppDelegate class before running.

Something like this:

Scene *HelloWorld::scene()
{
   Scene *scene = Scene::create();
   HelloWorld *layer = HelloWorld::create();

   layer->setTag(layer_tag);

   Sprite *blue_square = Sprite::create("blue_square.png");

   blue_square->setTag(bluesquare_tag);

   layer->addChild(blue_square);

   scene->addChild(layer);

   return scene;
}

class AppDelegate : private cocos2d::Application
{
...
private:
   Scene *_myscene;
}

bool AppDelegate::applicationDidFinishLaunching() 
{
   ....
   _myscene = HelloWorld::scene();

   director->runWithScene(_myscene);

   return true;
}

void AppDelegate::applicationWillEnterForeground() 
{
    CCDirector::sharedDirector()->startAnimation();
    ((Sprite *)(_myscene->getChildByTag(layer_tag)->getChildByTag(bluesquare_tag)))->setVisible(false);
}

Thanks Alexander! I already knew that was not an engine problem. I’m new to c++ and cocos2d so, I’m sorry for stupid errors. However now all works well. Thank you so much. :wink: