当前位置: 首页 > news >正文

4399小程序banner广告和插屏广告

banner广告

// 获取真机设备像素比 const pixelRatio = gamebox.getSystemInfoSync().pixelRatio; // 定义 Banner 广告的宽高和位置 const width = 320 * pixelRatio; const height = 50 * pixelRatio; const bannerLeft = (gamebox.getSystemInfoSync().screenWidth * pixelRatio - width) / 2; const bannerTop = gamebox.getSystemInfoSync().screenHeight * pixelRatio - height; let currentBannerAd = null; function createBannerAd() { return gamebox.createBannerAd({ style: { width: width, height: height, left: bannerLeft, top: bannerTop } }); } function showBannerAd() { if (currentBannerAd) { currentBannerAd.destroy(); // 销毁之前的 Banner 实例 } currentBannerAd = createBannerAd(); // 监听 Banner 广告加载事件回调函数 const bannerOnLoadCb = (res) => { console.info('Banner onLoad', res); }; // 监听 Banner 广告错误事件回调函数 const bannerOnErrorCb = (res) => { console.info('Banner onError', res); }; // 监听 Banner 广告加载事件 currentBannerAd.onLoad(bannerOnLoadCb); // 取消监听 Banner 广告加载事件(监听取消的函数,应与监听回调函数为同一个) currentBannerAd.offLoad(bannerOnLoadCb); // 监听 Banner 广告错误事件 currentBannerAd.onError(bannerOnErrorCb); // 取消监听 Banner 广告错误事件(监听取消的函数,应与监听回调函数为同一个) currentBannerAd.offError(bannerOnErrorCb); // 显示 Banner 广告 currentBannerAd.show().catch(err => { console.info('Banner 广告显示失败', err); }); } // 初始展示 Banner 广告 showBannerAd(); // 设置定时器,每2分钟重新展示 Banner 广告 setInterval(showBannerAd, 2 * 60 * 1000);

插屏广告

// 创建插屏广告实例 const interstitialAd = gamebox.createInterstitialAd(); // 监听 InterstitialAd 广告加载事件回调函数 const onLoadCb = (res) => { console.info('InterstitialAd onLoad ' + res); } // 监听 InterstitialAd 广告关闭事件回调函数 const onCloseCb = (res) => { console.info('InterstitialAd onClose ' + res); } // 监听 InterstitialAd 广告错误事件回调函数 const onErrorCb = (res) => { console.info('InterstitialAd onError ' + res); } // 监听 InterstitialAd 广告加载事件 interstitialAd.onLoad(onLoadCb); // 取消监听 InterstitialAd 广告加载事件(监听取消的函数,应与监听回调函数为同一个) interstitialAd.offLoad(onLoadCb); // 监听 InterstitialAd 广告关闭 interstitialAd.onClose(onCloseCb); // 取消监听 InterstitialAd 广告关闭(监听取消的函数,应与监听回调函数为同一个) interstitialAd.offClose(onCloseCb); // 监听 InterstitialAd 广告错误 interstitialAd.onError(onErrorCb); // 取消监听 InterstitialAd 广告错误(监听取消的函数,应与监听回调函数为同一个) interstitialAd.offError(onErrorCb); // 定义一个函数用于显示广告 function showInterstitialAd() { interstitialAd.show().catch(() => { // 失败重试 interstitialAd.load() .then(() => interstitialAd.show()) .catch(err => { console.log('InterstitialAd 广告显示失败', err); }); }); } // 设置一个定时器,每6分钟显示一次广告 setInterval(showInterstitialAd, 6 * 60 * 1000); // 6分钟转换为毫秒 // 销毁 InterstitialAd 广告 // interstitialAd.destroy(); // 如果需要在某些情况下销毁广告可以调用此方法
http://www.cnnetsun.cn/news/10075.html

相关文章:

  • Adobe官方卸载工具下载安装保姆级教程(附下载地址,非常详细)
  • shell笔记
  • 多头和q,kv的区别
  • 为什么加上位置编码后 patch 会有空间信息 需要解释一下
  • 基于Springboot船舶监造管理系统【附源码+文档】
  • 从原型到产品:融合算子的单元测试、集成测试与持续集成
  • Servlet原理Mapping问题ServletContext对象
  • 军事图像分类检测数据集介绍-351张图片 军事身份识别 安全检查辅助 智能监控系统 军事训练分析 历史军事影像分析
  • 人机环境系统智能是新理科与新文科的融点
  • 【字节开源Golang框架Eino】技术详解:架构原理+实战落地+避坑指南(附代码)
  • UE5 材质-22:
  • WebRTC 中的临界锁实现:从 CritScope 到 RAII 机制的深度解析
  • Mistral AI发布Magistral 1.2:24B参数轻量级模型重构多模态推理范式
  • Linux内核伙伴系统(Buddy System)原理详解
  • Universal x86 Tuning Utility终极指南:轻松解锁硬件性能的秘密武器
  • 基于三电平逆变器的有源滤波APF设计及Matlab/Simulink仿真
  • NVIDIA显卡深度调优指南:解锁隐藏性能的完全攻略
  • B站视频转文字完整指南:一键提取语音内容神器
  • 5步搞定Blender 3MF插件:从安装到高效3D打印工作流
  • Vue-Office Excel预览异常排查:从空白页面到完美渲染的完整指南
  • SQL SELECT:向数据库“点菜”的神奇指令
  • 深度学习之常用激活函数
  • 2023年IEEE TIV,GA-LNS算法+直升机救援调度,深度解析+性能实测
  • xshell的一个会话的连接的ip地址在哪里修改?
  • 【活动总结】创药沙龙第一期:ADC药物研发的挑战与机遇成功举办
  • 如何用免费工具3分钟终极优化Windows右键菜单:告别杂乱,提升300%操作效率
  • Day25
  • 工具 | netcat, netstat
  • AI的下半场:智能体(Agent)将如何重塑我们所有的应用
  • soular全面介绍(4) - 通过soular工作台聚合TikLab所有工具链