cocosCreator监听游戏是否置于后台

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import { game, Game } from "cc";

export class HideOrShow {
public static onLoad() {
game.on(Game.EVENT_HIDE, this.onAppHide, this);
game.on(Game.EVENT_SHOW, this.onAppShow, this);
}

public static onAppHide() {
console.log("应用进入后台");
}
public static onAppShow() {
console.log("应用回到前台");
}

public static onDestroy() {
game.off(Game.EVENT_HIDE, this.onAppHide, this);
game.off(Game.EVENT_SHOW, this.onAppShow, this);
}
}

然后在应用打开的时候调用上面的onLoad函数就可以啦,我们去看看日志吧~