cocosCreator监听游戏是否置于后台

2024-01-17 09:40:13 浏览数 (2)

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函数就可以啦,我们去看看日志吧~

0 人点赞