cocos creator 广告控制脚本

cocos creator 广告控制脚本

广告控制


const State = {
    fail: 0,
    succes: 1,
};
cc.Class({
    extends: cc.Component,

    properties: {

    },

    //打开右上角3个点转发
    openShare() {
        if (typeof (wx) !== "undefined") {
            //显示当前页面右上角的转发按钮
            wx.showShareMenu({ withShareTicket: true })
            wx.onShareAppMessage(function () {
                return {
                    title: '',
                    imageUrl: '' // 图片 URL
                }
            });
        };
    },

    //手动拉起转发调用
    manualShare: function (tag) {
        if (typeof (wx) !== "undefined") {
            //记录是什么分享
            this.shareTag = tag;
            this.shareTime = new Date().getTime(); // 记录当前时间
            //随机分享
            let rNum = Math.floor(Math.random() * 3);
            switch (rNum) {
                case 0:
                    wx.shareAppMessage({
                        title: "",
                        imageUrl: "",
                    });
                    break;
                case 1:
                    wx.shareAppMessage({
                        title: "",
                        imageUrl: "",
                    });
                    break;
                case 2:
                    wx.shareAppMessage({
                        title: "",
                        imageUrl: "",
                    });
                    break;
            };
        };

    },
    //微信回到前台分享判断
    shareJudge() {
        if (typeof (wx) !== "undefined") {
            let self = this;
            wx.onShow(function () {
                //标签不能为空
                if (self.shareTag !== null) {
                    let nowTime = new Date().getTime();
                    if (nowTime - self.shareTime >= 3000) {
                        self.shareSucces();
                    } else {
                        self.shareFail();
                    };
                } else {
                    return;
                }
            });
        }
    },
    //分享成功
    shareSucces() {
        this.shareState = State.succes;
        this.gameScene_js.createTipsUI("shareSucces");
    },

    //分享失败
    shareFail() {
        this.iniShare();
        this.gameScene_js.createTipsUI("shareFail");
    },

    //初始化分享
    iniShare() {
        this.shareTag = null; //分享的标签
        this.shareTime = null; //分享时的时间
        this.shareState = State.fail;
    },
    //初始化视频
    iniVideo() {
        this.videoTag = null;
        this.videoState = null;
    },
    //===========================================================
    //===========================================================
    //创建视频广告
    createvideoAd() {
        if (typeof (wx) !== "undefined") {
            //视频广告
            this.videoAd = wx.createRewardedvideoAd({
                adUnitId: '',//填上你的广告位id
            });

            //=======================================================
            //=======================================================

            //错误提示
            this.videoAd.onError(err => {
                this.AdManager = false;
                console.log(err)
            });
        };
    },
    //展示视频广告
    showvideoAd(name) {
        if (typeof (wx) !== "undefined") {
            if (this.AdManager == false) {
                //广告未开启
            } else {
                this.soundControl.pause_all_sound();
            };
            this.videoTag = name;
            //无论有多少广告,只会返回一个实例
            this.videoAd.show().catch(() => {
                // 失败重试
                this.videoAd.load()
                    .then(() => this.videoAd.show(),
                    )
                    .catch(err => {
                        console.log('激励视频 广告显示失败')
                        this.gameScene_js.createTipsUI("video_wait");
                    })
            })
        };
    },
    //广告结束或退出
    overvideoAd() {
        if (typeof (wx) !== "undefined") {
            this.videoAd.onClose(res => {
                // 用户点击了【关闭广告】按钮
                // 小于 2.1.0 的基础库版本,res 是一个 undefined
                if (res && res.isEnded || res === undefined) {
                    // 正常播放结束,可以下发游戏奖励
                    this.videoState = State.succes;
                    this.soundControl.resumeAllSound();
                }
                else {
                    // 播放中途退出,不下发游戏奖励
                    this.soundControl.resumeAllSound();
                    this.videoState = State.fail;
                    this.gameScene_js.createTipsUI(this.gameScene_js.node, "videoFail");
                }
            })
        };
    },
    //创建bannerAd广告
    createBannerAd() {
        if (typeof (wx) !== "undefined") {
            // 创建 Banner 广告实例,提前初始化
            let sysInfo = wx.getSystemInfoSync()
            this.bannerAd = wx.createBannerAd({
                adUnitId: '',
                adIntervals: 30,
                style: {
                    left: 0,
                    top: 0,
                    width: 300,
                }
            })
            this.bannerAd.onError(err => {
                console.log(err)
                // this.show_push_ad("bannerAd");
            });
            this.bannerAd.onResize(res => {
                this.bannerAd.style.top = sysInfo.windowHeight - this.bannerAd.style.realHeight - 10;
                this.bannerAd.style.left = (sysInfo.windowWidth - this.bannerAd.style.realWidth) / 2;
            })
        };//end if

    },

    //展示bannerAd
    showBannerAd() {
        cc.log("create_bannerAdAD");
        if (typeof (wx) !== "undefined") {

            this.bannerAd.show();
        };
    },
    //隐藏bannerAd
    hideBannerAd() {
        cc.log("hide_bannerAdAD");
        if (typeof (wx) !== "undefined") {
            this.bannerAd.hide();
        };
    },
    onLoad() {
        // cc.game.addPersistRootNode(this.node);
        this.gameScene_js = cc.find("Canvas").getComponent("gameScene");
        this.soundControl = this.node.getComponent("soundControl");
        this.shareJudge();
        this.iniShare();
        this.createvideoAd();
        this.overvideoAd();
        this.createBannerAd();
        this.openShare();
    },

    start() {

    },

    // update (dt) {},
});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值