Search code examples
androidc++11cocos2d-x

Keep the phone screen ON in Cocos2dx


I created a game for Android using Cocos2DX 3.4. I'm using the acelerometer for the player to move around the screen so I don't need to touch the screen. The problem is that the screen turn off when I play for a while. I need to know how to keep the phone awake even if I don't touch the screen.


Solution

  • I found one solution, but I´m still waiting for a better one if any:

    Just added this to my CPP:

    #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    #include "../cocos2d/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h"
    #endif
    

    Then I added this init method to my main Scene:

    bool HelloWorld::init()
    {
        if ( !LayerColor::initWithColor( Color4B(204,204,204,255)))
        {
            return false;
        }
        #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
        setKeepScreenOnJni(true);
        #endif
       return true;
    }