How to handle mobile device screen off & on in cocos2d-js?

How to handle mobile device screen off & on in cocos2d-js?
0.0 0

#1

Hi, I’m working on cocos2dx-js’s html5 project using cocos2dx 3.12 version.

How to handle event when user press physic “lock” button on device (or user switch from my game to home screen or another app) that put my game into sleep state. I need the event to pause my game on sleep & let user resume it manually.

Thanks.


#2

Hi,

// For event when the app entering background
cc.game.on(cc.game.EVENT_HIDE, function () {

});

// For event when the app entering foreground
cc.game.on(cc.game.EVENT_SHOW, function () {
// …
});

Regards,


#3

Hi there, thanks for your reply.

In which class should I write these methods.

I’m getting following error:-1:
TypeError: cc.game.on is not a function

thanks.


#4

Hi,

Its event listener. Try to add with custom event type in Javascript.

Example:

cc.eventManager.addCustomListener(cc.game.EVENT_HIDE, function () {
//Pause
});
cc.eventManager.addCustomListener(cc.game.EVENT_SHOW, function () {
//Resume
});

Regards,


#5

ok thank you for your help. It’s working.