How to call something when the application resume

How to call something when the application resume
0.0 0


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


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



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() 

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

void HelloWorld::doSomething()

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();


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




   return scene;

class AppDelegate : private cocos2d::Application
   Scene *_myscene;

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


   return true;

void AppDelegate::applicationWillEnterForeground() 
    ((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: