当前位置: 首页 > 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.rkmt.cn/news/84097.html

相关文章:

  • 搞单片机的简单吗?
  • 仅半年,半月回本的幻梦破灭,机器人的泡沫破灭得如此之快!
  • MySQL表的约束
  • 仅限资深开发者:C#编写高性能数字孪生渲染引擎的10个核心秘诀
  • OTOFIX D1 PRO 1-Year Online Update Subscription: Keep European/American Car Repairs Up-to-Date
  • 为什么顶级团队都在用Dify集成Spring AI?揭秘背后的架构优势
  • 从 “手动搬砖“ 到 “自动施法“:界面开发的三次 “渡劫“ 升级记
  • 基于Springboot线上装修管理系统【附源码+文档】
  • 从零到一构建量子计算环境,Docker镜像制作全流程详解
  • BepInEx插件框架终极指南:从零精通Unity游戏模组开发全流程
  • FastAIE:让 AI 不止于 “说”,更能 “动手” 的轻量级桌面执行工具
  • 四份 AI Agent 报告大比拼:为什么 AiPy 自研模型更懂行业?
  • ViGEmBus深度解析:游戏控制器虚拟化技术实战手册
  • Wan2.2-T2V-A14B在量子计算原理演示中的抽象概念具象化
  • XUnity.AutoTranslator:让Unity游戏秒变中文的智能翻译神器
  • 100 天学会爬虫 · Day 8:如何用 XPath 高效定位网页元素?(爬虫最常用解析方式之一)
  • 哔哩下载姬DownKyi:重新定义B站视频下载体验的智能解决方案
  • 天远全国自然人多头借贷风险API接口Java对接与解密工具类实现
  • 人机环境系统智能是新理科与新文科的融点
  • 【从零构建智能手写识别系统】:Dify平台下Tesseract 5.3深度配置与模型微调全流程
  • 流水的CEO,铁打的图里琛:中层管理者的“顶级钝感力”
  • Git 面试题全面汇总
  • 英雄联盟智能助手League Akari:从入门到精通的完整教程
  • AI智能改写助力:精选7款论文查重工具及可视化报告生成
  • C语言----三子棋
  • 数据结构(C语言版)树 二叉树
  • 通义千问Qwen重磅发布Qwen-Image-Edit:开创图像编辑“语义+外观“双控新纪元
  • 消費不是答案,但祛魅得先消費
  • Unity游戏翻译终极指南:XUnity.AutoTranslator完全掌握
  • 一线大厂测试开发岗位面试经验与真题解析(2025年12月版)