oppo小游戏接入代码

oppo接入

  • oppo获取用户信息
  • oppo广告接入

oppo获取用户信息

var self = this;
        qg.login({
            success: function (res) {
                //用户uid res.data.uid;
                //用户头像地址 res.data.avatar;
            },
            fail: function (res) {
                // errCode、errMsg
                console.log(JSON.stringify(res));
            }
        });

//注:根据地址获取图片资源
 var self = this;
        cc.loader.load(url, function (err, texture) {
            if (err) {
                console.log(err);
            } else {
                console.log(texture);
                var spriteFrame = new cc.SpriteFrame(texture);
                self.playerHeadSprite = spriteFrame;//得到的spriteFrame赋值给playerHeadSprite 
            }
        });

oppo广告接入

import GameController from "./GameController";//导入脚本GameController
import { AudioUtils } from "./AudioUtils";//导入脚本AudioUtils 


const {ccclass, property} = cc._decorator;

//定义枚举   看激励视频的类型
export enum AdvType {
    FreeGold,
    DoubleReward,
    AvoidLoss,
    FreePower,
    PowerMax,
}

@ccclass
export default class AdvManager extends cc.Component {

    public adv: string = "oppo";

    public advType: AdvType = AdvType.FreeGold;//看激励视频的类型

    onLoad() {
        if (this.adv == "oppo") {
            this.init();
        }
    }

    showInterstitialAD() {
        if (this.adv == "oppo") {
            //判断插屏间隔是否达到3分钟
            let oldDate = cc.sys.localStorage.getItem("InsertAdTime");
            if (oldDate != null) {
                //结束时间
                let now = new Date();
                //时间差的毫秒数
                let diff = now.getTime() - oldDate;
                if (diff < 3 * 60 * 1000) {
                    return;
                } else {
                    cc.sys.localStorage.setItem("InsertAdTime", now.getTime());
                }
            } else {
                let now = new Date();
                cc.sys.localStorage.setItem("InsertAdTime", now.getTime());
            }
            //播放插屏
            this.insertAd.show();
        }
    }

    showVideoAD() {
        AudioUtils.instance.pauseAll();
        //this.showVideoCallBack();
        //this.noVideoToShow();
        if (this.adv == "oppo") {
            if (true == this.isVideoAdLoaded) {
                this.videoAd.show();
            } else {
                this.noVideoToShow();
                this.videoAd.load();
            }
        }
    }

    showVideoCallBack() {
        AudioUtils.instance.resumeAll();
        if (this.advType == AdvType.FreeGold) {
        }
        else if (this.advType == AdvType.DoubleReward) {
            
        }
        else if (this.advType == AdvType.AvoidLoss) {
            
        }
        else if (this.advType == AdvType.FreePower) {
            
        }
        else if (this.advType == AdvType.PowerMax) {

        }
    }
    noVideoToShow() {
        AudioUtils.instance.resumeAll();
        GameController.ins.showNoAdv();
    }





    //oppo广告

    bannerAd: any = null;
    isBannerAdLoaded: boolean = false;

    insertAd: any = null;
    isInsertAdLoaded: boolean = false;

    videoAd: any = null;
    isVideoAdLoaded: boolean = false;


    init() {
        var self = this;
        qg.initAdService({
            appId: "",//appID
            isDebug: false,
            success: function (res) {
                console.log("success");

            },
            fail: function (res) {
                console.log("fail:" + res.code + res.msg);
            },
            complete: function (res) {
                console.log("complete");
                self.initBanner();
                self.initInsertAd();
                self.initVideoAd();
            }
        })

    }

    initBanner() {
        this.bannerAd = qg.createBannerAd({
            posId: ""//横幅广告id

        })
        var self = this;

        this.bannerAd.onShow(function (err) {
            console.log("bannerAd加载成功");
        })

        this.bannerAd.onError(function (err) {
            console.log("bannerAd onError");
            //self.showBannerSync();
        })
        this.scheduleOnce(function () {
            self.showBanner();
        }, 2);
    }

    showBanner() {
        this.bannerAd.show();
    }

    initInsertAd() {
        var self = this;
        this.insertAd = qg.createInsertAd({
            posId: ""//插屏id
        })
        this.insertAd.onLoad(function () {
            console.log("插屏广告加载成功");
            self.isInsertAdLoaded = true;
        })
        this.insertAd.onError(function (err) {
            console.log("insertAd onError");
            //self.loadInsertAdSync();
        })
        this.loadInsertAd();
    }

    loadInsertAd() {
        this.insertAd.load();
    }



    initVideoAd() {
        var self = this;
        this.videoAd = qg.createRewardedVideoAd({
            posId: ""//激励视频id
        })
        this.videoAd.onLoad(function () {
            console.log("激励视频加载成功");
            self.isVideoAdLoaded = true;
        })
        this.videoAd.onError(function () {
            console.log("激励视频加载失败");
            //self.loadVideoAdSync();
        })
        this.videoAd.load();

        this.videoAd.onClose((res) => {
            if (res.isEnded) {
                console.log('激励视频广告完成,发放奖励')
                this.showVideoCallBack();
            } else {
                console.log('激励视频广告取消关闭,不发放奖励')
                AudioUtils.instance.resumeAll();
            }
        })
    }



    loadVideoAd() {
        this.videoAd.load();
    }
}

你可能感兴趣的:(cocos)