Book Image

Building Android Games with Cocos2d-x

By : Raydelto Hernandez
Book Image

Building Android Games with Cocos2d-x

By: Raydelto Hernandez

Overview of this book

Table of Contents (16 chapters)
Building Android Games with Cocos2d-x
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Keeping the screen alive


In the previous section we added the accelerometer interaction to our game, which means that our player is controlling the main character by moving the phone rather than by touching the screen, this will cause many android devices to turn off the screen after a period of inactivity (not touching the screen). Of course, no one would like that our Android device's screen suddenly goes black; in order to prevent this from happening, we will call the setKeepScreenOnJni method, introduced in the previous version 3.3 of the framework. Prior to this version, this annoying situation was considered a framework flaw, and now it has finally been fixed.

We first need to include the helper in our HelloWorldScene.cpp header file as follows:

#include "../cocos2d/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h"

Then, we are going to add the following line at the end of our init method in the HelloWorldScene.cpp implementation file:

setKeepScreenOnJni(true);