创建WxPlatform.ts文件

封装bannger广告

class WxPlatform {//banner广告banner: BannerAd;showBanner() {if (cc.sys.platform != cc.sys.WECHAT_GAME) {return;}if (this.banner != null) {this.banner.destroy();}let id = "填写流量主banner广告id";let winSize = wx.getSystemInfoSync();let bannerAd = wx.createBannerAd({adUnitId: id,adIntervals: 30,style: {height: winSize.windowHeight - 80,left: 0,top: 500,width: winSize.windowWidth,}})bannerAd.onResize(res => {bannerAd.style.top = winSize.windowHeight - bannerAd.style.realHeight;bannerAd.style.left = winSize.windowWidth / 2 - bannerAd.style.realWidth / 2;})bannerAd.onLoad(() => {console.log('banner 广告加载成功');this.banner = bannerAd;})bannerAd.onError(err => {console.log('banner 广告加载错误', err);})bannerAd.show();}}

调用banner广告

在调用的页面调用WxPlatform.ts文件,要调用的位置:

wxplatform.showBanner(); //显示banner

如果是多个页面都要调用,可以在其他页面判断banner是否存在

if (wxplatform.banner) {wxplatform.banner.hide();this.scheduleOnce(() => {//隔一秒显示wxplatform.banner.show();}, 1);} 

封装原生模板广告组件

class WxPlatform {//创建原生模板广告组件-五行格子广告-弹窗custom5: CustomAd;showCustomAd5(type, callback: Function) {let id = "";let winSize = wx.getSystemInfoSync();let style = {}if (type && type == 5) {id = "原生五行格子广告id";style = {width: winSize.screenWidth / 1.2,left: (winSize.screenWidth - winSize.screenWidth / 1.2) / 2,top: winSize.windowHeight / 10,fixed: 0,}}if (cc.sys.platform != cc.sys.WECHAT_GAME) {return;}if (this.custom5) {this.custom5.show();return;}let CustomAd = wx.createCustomAd({adUnitId: id,  //'adunit-abf9c63427c3a38a',style: style});CustomAd.onLoad(() => {console.log('原生模板 广告加载成功');callback(1);CustomAd.show().then(() => console.log('原生模板 广告显示成功')).catch((err) => {console.log("原生模板 广告显示失败", err)callback(0);})this.custom5 = CustomAd;})CustomAd.onClose(res => {console.log('关闭原生模板广告', res);});CustomAd.onError(err => {console.log('原生模板CustomAd 广告加载失败:', err);callback(0);});}
}

调用原生模板广告组件

在调用的页面调用WxPlatform.ts文件,要调用的位置:

    if(wxplatform.custom5){wxplatform.custom5.show();}else{wxplatform.showCustomAd5(5,(data)=>{if(data == 0){//如果失败了就关闭弹窗,这里看自己的逻辑是如何的this.node.destroy();//关闭}});}

封装插屏广告

class WxPlatform {showInterstitialAd() {if (cc.sys.platform != cc.sys.WECHAT_GAME) {return;}let interstitialAd = wx.createInterstitialAd({adUnitId: "插屏广告id",});interstitialAd.load().then(() => {console.log('插屏 广告加载成功');});interstitialAd.show().catch((err) => {console.log('插屏广告展示失败', err);});interstitialAd.onClose(res => {// console.log('关闭插屏广告', res);});interstitialAd.onError(err => {console.log('err:插屏广告加载失败', err);});}}

调用插屏广告

在调用的页面调用WxPlatform.ts文件,要调用的位置:

wxplatform.showInterstitialAd();

封装激励视频广告

class WxPlatform {rewardVideo2: RewardedVideoAd;showRewardVideo2(callback: Function) {if (cc.sys.platform != cc.sys.WECHAT_GAME) {callback(1);return;}let my = this;if (my.rewardVideo2 != null) {my.rewardVideo2.offClose(fun);}let rewardedVideoAd = wx.createRewardedVideoAd({adUnitId: "激励视频广告id",});my.rewardVideo2 = rewardedVideoAd;rewardedVideoAd.load().then(() => {console.log('激励视频 广告加载成功');rewardedVideoAd.show();});rewardedVideoAd.onError(err => {console.log('激励视频 广告显示失败', err);callback(2);})var fun = function (res) {if (res && res.isEnded) {console.log('res:  ', res);callback(1);rewardedVideoAd.offClose(fun);} else {console.log('播放中途退出');callback(0);}}rewardedVideoAd.onClose(fun);}}

调用激励视频广告

在调用的页面调用WxPlatform.ts文件,要调用的位置:

wxplatform.showRewardVideo2((data) => {//通过data判断激励视频广告是否加载成功、失败;判断是否中途退出还是完整播放if(data == 1){//激励视频完整播放 需要做的奖励;}})

cocos creator 微信小游戏 封装banner、原生格子、插屏、激励视频广告相关推荐

  1. Cocos Creator微信小游戏添加banner广告

    学了小游戏开发也有几个月了,这边写一下这几个月以来遇到的坑. 首先是添加banner广告 看过很多个写微信广告的写法.感觉其实是在误导萌新,这是一般别人的例子 let winSize = wx.get ...

  2. cocos creator 微信小游戏接口

    cocos creator 微信小游戏接口 /** @Description:微信小游戏* @version: * @Author: lvpeijun* @Date: 2019-12-02 10:43 ...

  3. 解决cocos creator微信小游戏本地缓存文件超过限制问题

    cocos creator微信小游戏开发,如果游戏包内资源超过包体大小限制,那么可以通过远程加载的方式加载资源(代码脚本文件除外). 小游戏在迭代过程中因为缓存资源的积累,在加载资源的时候会出现超过缓 ...

  4. 微信小游戏 资源服务器,Cocos Creator 微信小游戏 远程资源设置

    版本:2.3.4 参考: cocos教程:发布到微信小游戏 因为微信小游戏有包体大小限制. 主包4M 分包最大4M 一共最多16M 那么假如我们的游戏有个20M,50M,那就放不下了.只能放到远程服务 ...

  5. Cocos Creator 微信小游戏 分包设置

    版本:2.3.4 参考: cocos教程:分包加载 在微信小游戏中使用分包.因为微信小游戏有包体限制. 主包最大只能4m,分包最大4m,一共最多16m.所以主包只放代码,而图片.json等资源都设置为 ...

  6. Cocos Creator 微信小游戏 填坑记录

    环境:(ccc-v1.9.1, wx-v1.02.1804251) 微信小游戏禁止了动态生成并执行代码的功能,window.eval(). new Function() ,这些都不能用了. 微信小游戏 ...

  7. Cocos Creator微信小游戏添加视频广告

    视频广告的添加可以先参考一下微信视频广告api 和创建banner广告一样.我们先写一个Common.js, export = {videoBar_1: null,videoBar2 : null, ...

  8. Cocos Creator 微信小游戏苹果手机锁屏卡死的坑

    在制作小游戏项目时遇到了 苹果系列手机 只要游戏过程中进行锁屏后解锁时,整个手机或微信会卡个几秒甚至会黑屏! 所以就这个问题进行了各种 onshow/onhide 的操作试验. 第一种:在onshow ...

  9. Cocos Creator 微信小游戏无法正确显示头像 解决方案

    常见的获取微信头像方法 UserInfo:  用户授权成功后 返回的微信用户信息 UserInfo 结构体信息详情: 进入 微信如何登陆授权: 进入 let url = userInfo.avatar ...

最新文章

  1. mysql 存byte数组中_新人求大神教教:如何把一个一维数组存入mysql 表格中
  2. php多文件压缩的功能函数
  3. linux e514写入错误,Linux上使用vim编辑文件保存时报错:E514: write error (file system full?)...
  4. codeforces1451 D. Circle Game
  5. 李洋疯狂C语言之求素数的方法
  6. C++PrimerPlus学习——第十一章编程练习
  7. 基于神经网络的文本分类(基于Pytorch实现)
  8. 安装Bootstrap3源码版本
  9. 湖北省月降水量分布数据
  10. 宋宝华《Linux设备驱动开发详解》
  11. 新型的铁塔基站“能源管家”
  12. Arduino库文件LiquidCrystal的实用方法
  13. 防火墙(NGFW)与UTM本质上的区别
  14. html隐藏手机状态栏,如何隐藏iPhone手机状态栏_隐藏iPhone手机状态栏操作方法介绍-果粉控...
  15. SAP UI5 应用开发教程之八十三 - SAP UI5 的自动化测试套件页面的开发步骤介绍试读版
  16. 李航统计学习感知机算法实现
  17. 《炬丰科技-半导体工艺》RCA清洗系统及清洗液自适应预测温度
  18. pap认证失败_PPP PAP(CHAP)认证
  19. MyBatis 03 动态SQL
  20. Flash Timeout.Reset the Target and try it again 报错解决方案 STM32 ST-LINK Utility下载程序操作

热门文章

  1. 《动手学深度学习》13.4锚框
  2. 青龙面板小黄鸟抓包软件,断网模块,vmos虚拟机
  3. Kubernetes参数:dryRun理解
  4. 嵌入式常问问题和知识
  5. python socket tcp6_渗透技巧——使用远程桌面协议建立通道
  6. 计算机论文展望未来,计算机本科毕业论文参考例文.docx
  7. 什么是Qi2?新无线充电标准解释
  8. 运用UltraISO制作系统启动盘(U盘)
  9. 【数据结构与算法】二维数组中的查找(剑指offer)java版
  10. buuoj Pwn writeup 186-190