cocosCreator监听游戏是否置于后台
南锋2024-01-162024-01-16(adsbygoogle=window.adsbygoogle||[]).push({})
代码
代码语言:javascript复制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
函数就可以啦,我们去看看日志吧~