369 lines
11 KiB
JavaScript
369 lines
11 KiB
JavaScript
// Learn cc.Class:
|
||
// - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
|
||
// - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
|
||
// Learn Attribute:
|
||
// - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
|
||
// - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
|
||
// Learn life-cycle callbacks:
|
||
// - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
|
||
// - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
|
||
|
||
module.exports = {
|
||
//extends: cc.Component,
|
||
|
||
properties: {
|
||
// foo: {
|
||
// // ATTRIBUTES:
|
||
// default: null, // The default value will be used only when the component attaching
|
||
// // to a node for the first time
|
||
// type: cc.SpriteFrame, // optional, default is typeof default
|
||
// serializable: true, // optional, default is true
|
||
// },
|
||
// bar: {
|
||
// get () {
|
||
// return this._bar;
|
||
// },
|
||
// set (value) {
|
||
// this._bar = value;
|
||
// }
|
||
// },
|
||
|
||
},
|
||
|
||
// LIFE-CYCLE CALLBACKS:
|
||
|
||
// onLoad () {},
|
||
|
||
|
||
|
||
__createAD: function(adtype, adunitid, bannerY, canvasH, owner){
|
||
let obj = owner;
|
||
if(!owner){
|
||
obj = {
|
||
adType: adtype,
|
||
adUnitId: adunitid,
|
||
state: 0
|
||
};
|
||
}
|
||
switch(this.channelid){
|
||
case 6001:
|
||
{
|
||
if(adtype == 0){
|
||
let realy = this.winH / 2 - bannerY / canvasH * this.winH - 80 / 2;
|
||
if(!obj.style){
|
||
obj.style = {
|
||
left: 0,
|
||
top: realy,
|
||
width: this.winW,
|
||
height: 80,
|
||
//realWidth: nc.width,
|
||
//realHeight: nc.height
|
||
};
|
||
}
|
||
let data = {
|
||
adUnitId: adunitid,
|
||
style: obj.style
|
||
};
|
||
if(wx && wx.createBannerAd){
|
||
let ad = wx.createBannerAd(data);
|
||
obj.ad = ad;
|
||
}
|
||
/*
|
||
ad.onResize(res => {
|
||
console.log("[bannerad]onResize:" + res.width + "," + res.height);
|
||
console.log("[bannerad]style:" + JSON.stringify(ad));
|
||
});
|
||
*/
|
||
}else if(adtype == 1){
|
||
let data = {
|
||
adUnitId: adunitid
|
||
};
|
||
if(wx && wx.createRewardedVideoAd){
|
||
let ad = wx.createRewardedVideoAd(data);
|
||
obj.ad = ad;
|
||
}
|
||
}
|
||
}break;
|
||
case 6002:
|
||
{
|
||
if(adtype == 0){
|
||
let realy = this.winH / 2 - bannerY / canvasH * this.winH - 80 / 2;
|
||
if(!obj.style){
|
||
obj.style = {
|
||
x: 0,
|
||
y: realy
|
||
//realWidth: nc.width,
|
||
//realHeight: nc.height
|
||
};
|
||
}
|
||
let data = {
|
||
viewId: adunitid,
|
||
style: obj.style
|
||
};
|
||
if(BK && BK.Advertisement && BK.Advertisement.createBannerAd){
|
||
let ad = BK.Advertisement.createBannerAd(data);
|
||
obj.ad = ad;
|
||
}
|
||
}else if(adtype == 1){
|
||
if(BK && BK.Advertisement && BK.Advertisement.createVideoAd){
|
||
let ad = BK.Advertisement.createVideoAd();
|
||
obj.ad = ad;
|
||
}
|
||
}
|
||
}break;
|
||
}
|
||
return obj;
|
||
},
|
||
|
||
init(channelid, gameid, isoffical, owner){
|
||
this.owner = owner;
|
||
this.gameid = gameid;
|
||
this.channelid = channelid;
|
||
this.BannerAds = [];
|
||
this.VideoAds = [];
|
||
this.cleanBannerAD();
|
||
this.cleanVideoAD();
|
||
},
|
||
|
||
setAccountID(accountid, sessionid){
|
||
this.accountid = accountid;
|
||
this.sessionid = sessionid;
|
||
},
|
||
|
||
setNickName(nickname){
|
||
this.nickname = nickname;
|
||
},
|
||
|
||
setFromAppID(appid){
|
||
this.fromid = appid;
|
||
},
|
||
|
||
setLocalUUID(uuid){
|
||
this.localid = uuid;
|
||
},
|
||
|
||
setSystemInfo(info){
|
||
this.winW = info.windowWidth;
|
||
this.winH = info.windowHeight;
|
||
},
|
||
|
||
// 添加banner广告
|
||
addBannerAD(advid, identifier, bannerY, canvasH, rspCb){
|
||
var adobj = this.__createAD(0, advid, bannerY, canvasH);
|
||
if(adobj){
|
||
adobj.param = identifier;
|
||
adobj.rspCb = rspCb;
|
||
this.initAD(adobj);
|
||
this.BannerAds.push(adobj);
|
||
}
|
||
return ad;
|
||
},
|
||
|
||
// 添加激励广告(视频广告)
|
||
addVideoAD(advid, identifier, rspCb){
|
||
var adobj = this.__createAD(1, advid);
|
||
if(adobj){
|
||
adobj.param = identifier;
|
||
adobj.rspCb = rspCb;
|
||
this.initAD(adobj);
|
||
this.VideoAds.push(adobj);
|
||
}
|
||
},
|
||
|
||
// update (dt) {},
|
||
|
||
reLoadBannerAD(identifier){
|
||
let obj = this.BannerAds.find((element) => {
|
||
return element.param == identifier;
|
||
})
|
||
|
||
if(obj && obj.ad && obj.style){
|
||
let id = obj.adUnitId;
|
||
this.destoryAD(obj.ad);
|
||
this.__createAD(0, id, 0, 0, obj);
|
||
}
|
||
return obj;
|
||
},
|
||
|
||
hasBannerAD(identifier){
|
||
let obj = this.findBannerAD(identifier);
|
||
if(obj && obj.ad){
|
||
return true;
|
||
}
|
||
return false;
|
||
},
|
||
|
||
hasVideoAD(identifier){
|
||
let obj = this.findVideoAD(identifier);
|
||
if(obj && obj.ad){
|
||
return true;
|
||
}
|
||
return false;
|
||
},
|
||
|
||
findBannerAD(identifier){
|
||
let obj = this.BannerAds.find((element) => {
|
||
return element.param == identifier;
|
||
})
|
||
return obj;
|
||
},
|
||
|
||
findVideoAD(identifier){
|
||
let obj = this.VideoAds.find((element) => {
|
||
return element.param == identifier;
|
||
})
|
||
return obj;
|
||
},
|
||
|
||
hideAllAD(){
|
||
this.BannerAds.forEach(element => {
|
||
this.hideAD(element);
|
||
});
|
||
},
|
||
|
||
// 显示/隐藏banner广告
|
||
switchBannerAD (identifier, bShow){
|
||
this.hideAllAD();
|
||
|
||
if(bShow){
|
||
let obj = reLoadBannerAD(identifier);
|
||
obj.needshow = true;
|
||
this.showAD(obj);
|
||
}
|
||
},
|
||
|
||
// 显示激励广告(视频广告)
|
||
showRewardAD (identifier){
|
||
let obj = this.findVideoAD(identifier);
|
||
if(!obj || !obj.ad){
|
||
return;
|
||
}
|
||
obj.needshow = true;
|
||
this.loadAD(obj);
|
||
},
|
||
|
||
onLoadAD(obj){
|
||
console.log("[ad]onload");
|
||
obj.rspCb && obj.rspCb(100, obj);
|
||
if(obj.needshow){
|
||
this.showAD(obj);
|
||
}
|
||
},
|
||
|
||
onErrAD(errtype, err, obj){
|
||
console.log('[AD]err:' + errtype);
|
||
console.log(err);
|
||
obj.needshow = false;
|
||
obj.rspCb && obj.rspCb(errtype, obj, err);
|
||
},
|
||
|
||
destoryAD(ad){
|
||
if(!ad){
|
||
return;
|
||
}
|
||
ad.state = 30;
|
||
ad.destroy && ad.destroy();
|
||
},
|
||
|
||
showAD(obj){
|
||
let ad = obj.ad;
|
||
if(!ad){
|
||
return;
|
||
}
|
||
ad.state = 10;
|
||
ad.show && ad.show();
|
||
},
|
||
|
||
hideAD(obj){
|
||
let ad = obj.ad;
|
||
if(!ad){
|
||
return;
|
||
}
|
||
ad.state = 20;
|
||
ad.hide && ad.hide();
|
||
},
|
||
|
||
// 激励广告调用
|
||
loadAD(obj){
|
||
let ad = obj.ad;
|
||
if(!ad){
|
||
return;
|
||
}
|
||
ad.state = 100;
|
||
switch(this.channelid){
|
||
case 6001:
|
||
{
|
||
ad.load && ad.load();
|
||
}break;
|
||
case 6002:{
|
||
this.onLoadAD(obj);
|
||
}break;
|
||
}
|
||
|
||
},
|
||
|
||
initAD(obj){
|
||
let ad = obj.ad;
|
||
if(!ad){
|
||
return;
|
||
}
|
||
ad.onLoad && ad.onLoad(() => {
|
||
console.log("[ad]onload");
|
||
this.onLoadAD(obj);
|
||
});
|
||
ad.onError && ad.onError(res => {
|
||
console.log("[ad]onerror:"+JSON.stringify(res));
|
||
this.onErrAD(-obj.state, res, obj);
|
||
});
|
||
switch(this.channelid){
|
||
case 6001:{
|
||
let closeCb = function(res){
|
||
console.log("[ad]onClose:"+JSON.stringify(res));
|
||
// 用户点击了【关闭广告】按钮
|
||
// 小于 2.1.0 的基础库版本,res 是一个 undefined
|
||
if (res && res.isEnded || res === undefined) {
|
||
// 正常播放结束,可以下发游戏奖励
|
||
obj.rspCb && obj.rspCb(10, obj, res, res.isEnded);
|
||
} else {
|
||
// 播放中途退出,不下发游戏奖励
|
||
obj.rspCb && obj.rspCb(11, obj, res, false);
|
||
}
|
||
// ad.offClose && ad.offClose(closeCb);
|
||
};
|
||
ad.onClose && ad.onClose(closeCb);
|
||
}break;
|
||
case 6002:{
|
||
let closeCb = function(res){
|
||
console.log("[ad]onClose:"+JSON.stringify(res));
|
||
obj.rspCb && obj.rspCb(11, obj, null, false);
|
||
// ad.offClose && ad.offClose(closeCb);
|
||
};
|
||
// ad.onPlayStart && ad.onPlayStart(function () {
|
||
// //开始播放
|
||
// });
|
||
ad.onPlayFinish && ad.onPlayFinish(function () {
|
||
//播放结束
|
||
obj.rspCb && obj.rspCb(10, obj, null, true);
|
||
});
|
||
ad.onClose && ad.onClose(closeCb);
|
||
}break;
|
||
}
|
||
|
||
},
|
||
|
||
cleanBannerAD(){
|
||
if(this.BannerAds.length > 0){
|
||
this.BannerAds.forEach(element => {
|
||
this.destoryAD(element.ad);
|
||
});
|
||
this.BannerAds.length = 0;
|
||
}
|
||
},
|
||
|
||
cleanVideoAD(){
|
||
if(this.VideoAds.length > 0){
|
||
this.VideoAds.length = 0;
|
||
}
|
||
}
|
||
};
|