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) {},
});