AdAgentQQ.ts 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. import AdAgent from "./AdAgent";
  2. import { BannerLocation } from "./YZ_Constant";
  3. import PlatUtils from "./PlatUtils";
  4. import { utils } from "./Utils";
  5. const { ccclass, property } = cc._decorator;
  6. @ccclass
  7. export default class AdAgentQQ extends AdAgent {
  8. _sysData: any = null;
  9. _curBannerAd: any = null;
  10. _videoAd: any = null;
  11. _insertAd: any = null;
  12. _oldAd: any = null;
  13. _appBox: any = null; //游戏盒子广告
  14. _videoCallback: Function = null;
  15. _isVideoLoaded: boolean = false;
  16. _isVideoShow: boolean = false;
  17. //@ts-ignore
  18. qq: any = window.qq;
  19. public Init() {
  20. if (PlatUtils.IsQQ) {
  21. utils.registerServerInitEvent(() => {
  22. this._sysData = utils.Tool_QQ.getSystemInfo();
  23. this._initVideoAd();
  24. }, this);
  25. }
  26. }
  27. public get ServerConfig() {
  28. return utils.Tool_QQ.ServerConfig;
  29. }
  30. // /**
  31. // * 初始化游戏盒子
  32. // */
  33. // _initAppBox() {
  34. // let checkVersion: boolean = utils.Tool_QQ.isOverMinVersion("1.7.1");
  35. // if (checkVersion) {
  36. // this._appBox = qq.createAppBox({
  37. // adUnitId: utils.config.qqconfig.appBoxId
  38. // });
  39. // this._appBox.load().then(() => {
  40. // utils.showLog("游戏盒子加载成功!")
  41. // }).catch(() => {
  42. // utils.showLog("游戏盒子加载失败!");
  43. // });
  44. // } else {
  45. // cc.warn("当前基础库版本低于1.7.1,不显示盒子广告");
  46. // }
  47. // }
  48. _initVideoAd() {
  49. if (!this._videoAd) {
  50. utils.showLog("初始化视频!");
  51. if (!utils.config.qqconfig.videoId) {
  52. utils.showLog("视频ID配置错误!");
  53. if (this._videoCallback) {
  54. this._videoCallback(false, "暂无视频广告!");
  55. this._videoCallback = null;
  56. }
  57. return;
  58. }
  59. // if (!utils.Tool_QQ.isOverMinVersion("0.1.26")) {
  60. // utils.showLog("当前版本不支持视频广告!");
  61. // if (this._videoCallback) {
  62. // this._videoCallback(false, "暂无视频广告!");
  63. // this._videoCallback = null;
  64. // }
  65. // return;
  66. // }
  67. utils.showLog("视频广告ID:", utils.config.qqconfig.videoId.trim());
  68. this._videoAd = this.qq.createRewardedVideoAd({
  69. adUnitId: utils.config.qqconfig.videoId
  70. });
  71. if (this._videoAd) {
  72. utils.showLog("初始化注册视频回调!");
  73. this._videoAd.onLoad(() => {
  74. utils.showLog("激励视频加载成功");
  75. this._isVideoLoaded = true;
  76. if (this._isVideoShow) {
  77. this._videoAd.show().then(() => {
  78. }).catch(() => {
  79. utils.showLog("视频播放失败!");
  80. if (this._videoCallback) {
  81. this._videoCallback("暂无视频广告!");
  82. this._videoCallback = null;
  83. }
  84. });
  85. }
  86. });
  87. this._videoAd.onError((err) => {
  88. utils.showLog("激励视频加载失败!", err.code, err.msg);
  89. this._isVideoLoaded = false;
  90. if (this._videoCallback) {
  91. this._videoCallback(false, "暂无视频广告!");
  92. this._videoCallback = null;
  93. }
  94. });
  95. this._videoAd.onClose((res) => {
  96. this._isVideoShow = false;
  97. if (res && res.isEnded || res === undefined) {
  98. // 正常播放结束,可以下发游戏奖励
  99. utils.showLog('激励视频广告完成,发放奖励');
  100. if (this._videoCallback) {
  101. this._videoCallback(true, "");
  102. this._videoCallback = null;
  103. }
  104. } else {
  105. // 播放中途退出,不下发游戏奖励
  106. utils.showLog('激励视频广告取消关闭,不发放奖励');
  107. if (this._videoCallback) {
  108. this._videoCallback(false, "观看完视频才能获得奖励!");
  109. this._videoCallback = null;
  110. }
  111. }
  112. });
  113. } else {
  114. utils.showLog("激励视频初始化失败!");
  115. if (this._videoCallback) {
  116. this._videoCallback(false, "暂无视频广告!");
  117. this._videoCallback = null;
  118. }
  119. }
  120. }
  121. }
  122. _bannerShow: boolean = false;
  123. _bannerSizePercent: number = 0.5;
  124. _bannerBottom: number = 0;
  125. _oldBannerLocation: BannerLocation = BannerLocation.None;
  126. _curBannerHeight: number = 240;
  127. _moveBtn: cc.Node = null;
  128. _cur_level: any = null;
  129. _createBanner(location: BannerLocation, args: any = null) {
  130. if (PlatUtils.IsQQ) {
  131. if (!utils.config.qqconfig.bannerId) {
  132. utils.showLog("banner广告ID不存在");
  133. return;
  134. }
  135. var argsTmp: any = args;
  136. this._bannerShow = false;
  137. if (argsTmp && argsTmp.width) {
  138. this._bannerSizePercent = argsTmp.width < 0 ? 0 : argsTmp.width;
  139. this._bannerSizePercent = argsTmp.width > 1 ? 1 : this._bannerSizePercent;
  140. }
  141. if (argsTmp && argsTmp.bottom) {
  142. this._bannerBottom = argsTmp.bottom / this._sysData.pixelRatio;
  143. this._bannerBottom = this._bannerBottom < 0 ? 0 : this._bannerBottom;
  144. this._bannerBottom = this._bannerBottom > this._sysData.screenHeight ? this._sysData.screenHeight : this._bannerBottom;
  145. }
  146. // IOS平台不支持onResize更改尺寸,所以这里宽度固定最小值
  147. // let width = this._sysData.screenWidth * this._bannerSizePercent;
  148. let width = 300;
  149. let height = 72;
  150. let left = (this._sysData.screenWidth - width) / 2;
  151. let top = this._sysData.screenHeight - height - this._bannerBottom;
  152. let params = {
  153. adUnitId: utils.config.qqconfig.bannerId,
  154. style: {
  155. left: left,
  156. width: width,
  157. top: top,
  158. height: height,
  159. }
  160. };
  161. let bannerAd: any = null;
  162. let oldBannerAd: any = this._curBannerAd;
  163. bannerAd = this.qq.createBannerAd(params);
  164. utils.showLog("bannerAd ", bannerAd);
  165. if (bannerAd) {
  166. bannerAd.onError((err) => {
  167. utils.showLog("广告条加载失败! ", JSON.stringify(err));
  168. this._bannerShow = false;
  169. // if (location == BannerLocation.Game && this.ServerConfig.banner_first_ad && this.ServerConfig.banner_first_ad == "1") {
  170. // utils.showLog("服务器配置banner广告优先级为默认banner,游戏页面备用显示积木广告!");
  171. // this.showBlockAd({ bottom: 0 });
  172. // }
  173. });
  174. bannerAd.onLoad(() => {
  175. if (this._bannerShow) {
  176. return;
  177. }
  178. this._bannerShow = true;
  179. try {
  180. bannerAd.show().then(() => {
  181. utils.showLog("Banner广告显示成功!");
  182. if (this.ServerConfig.banner_move_btn_interval && this._cur_level && this._cur_level % this.ServerConfig.banner_move_btn_interval == 0) {
  183. this.moveBtnToBannerTop();
  184. }
  185. if (oldBannerAd) {
  186. oldBannerAd.destroy();
  187. }
  188. this._curBannerAd = bannerAd;
  189. }).catch((err) => {
  190. utils.showLog("Banner广告出错", JSON.stringify(err));
  191. this._bannerShow = false;
  192. });
  193. } catch (error) {
  194. if (this.ServerConfig.banner_move_btn_interval && this._cur_level && this._cur_level % this.ServerConfig.banner_move_btn_interval == 0) {
  195. this.moveBtnToBannerTop();
  196. }
  197. utils.showLog("Banner ad Show erro", error);
  198. }
  199. });
  200. bannerAd.onResize((res: any) => {
  201. if (res) {
  202. this._curBannerHeight = res.height * this._sysData.pixelRatio;
  203. }
  204. });
  205. }
  206. }
  207. }
  208. _showBannerTimer(location: BannerLocation, args: any) {
  209. let locationTmp: BannerLocation = location;
  210. let argsTmp: any = args;
  211. utils.showLog(`显示Banner广告xxx!location:${locationTmp}; 间隔时间:${utils.Tool_QQ.ServerConfig.refresh_ad_time}:优先级:${this.ServerConfig.banner_first_ad}`);
  212. // if (this.ServerConfig.banner_first_ad) {
  213. // switch (this.ServerConfig.banner_first_ad) {
  214. // case "default":
  215. // this._createBanner(locationTmp, argsTmp);
  216. // break;
  217. // case "box":
  218. // if (!utils.Tool_QQ.isOverMinVersion("1.15.0")) {
  219. // utils.showLog("当前版本不支持积木广告,显示默认banner");
  220. // this._createBanner(locationTmp, argsTmp);
  221. // return;
  222. // }
  223. // this.showBlockAd();
  224. // break;
  225. // // case "3":
  226. // // this._createBanner(locationTmp, argsTmp);
  227. // // this.showBlockAd();
  228. // // break;
  229. // }
  230. // } else {
  231. let banner_delay_time = this.ServerConfig.banner_delay_time ? this.ServerConfig.banner_delay_time : 0;
  232. utils.showLog(`延迟${banner_delay_time}秒显示banner`);
  233. if (!this.ServerConfig.banner_move_btn_interval || !this._cur_level || this._cur_level % this.ServerConfig.banner_move_btn_interval != 0) {
  234. utils.showLog("当前关卡不做延迟移动!");
  235. this.moveBtnToBannerTop();
  236. }
  237. this._showBannerTimerId = setTimeout(() => {
  238. this._createBanner(locationTmp, argsTmp)
  239. }, banner_delay_time * 1000);
  240. // }
  241. }
  242. _showBannerTimerId: number = 0;
  243. public ShowBanner(location: BannerLocation = BannerLocation.Home, args: any = null) {
  244. if (PlatUtils.IsQQ) {
  245. if (utils.Tool_QQ.ServerConfig) {
  246. let locationTmp: BannerLocation = location;
  247. let argsTmp: any = args;
  248. this._moveBtn = args ? args.moveBtn : null;
  249. this._cur_level = args ? args.cur_level : null;
  250. this._showBannerTimer(locationTmp, argsTmp);
  251. } else {
  252. utils.showLog("服务器配置数据未初始化!");
  253. }
  254. }
  255. }
  256. public HideBanner(location: BannerLocation = BannerLocation.Home) {
  257. if (PlatUtils.IsQQ) {
  258. utils.showLog("隐藏广告条");
  259. this._bannerShow = false;
  260. clearInterval(this._showBannerTimerId);
  261. if (this._curBannerAd) {
  262. this._curBannerAd.hide();
  263. }
  264. }
  265. }
  266. public ShowVideo(callback: Function) {
  267. if (PlatUtils.IsQQ) {
  268. this._videoCallback = callback;
  269. this._isVideoShow = true;
  270. if (!this._videoAd) {
  271. this._initVideoAd();
  272. } else {
  273. this._videoAd.show().then(() => {
  274. utils.showLog("视频显示成功!");
  275. }).catch((err) => {
  276. utils.showLog("视频未加载!");
  277. this._videoAd.load();
  278. });
  279. }
  280. }
  281. }
  282. /**
  283. * 显示插屏
  284. * 2001 触发频率限制 小程序启动一定时间内不允许展示插屏广告
  285. * 2002 触发频率限制 距离小程序插屏广告或者激励视频广告上次播放时间间隔不足,不允许展示插屏广告
  286. * 2003 触发频率限制 当前正在播放激励视频广告或者插屏广告,不允许再次展示插屏广告
  287. * 2004 广告渲染失败 该项错误不是开发者的异常情况,或因小程序页面切换导致广告渲染失败
  288. * 2005 广告调用异常 插屏广告实例不允许跨页面调用
  289. * 销毁插屏广告后才能重新创建
  290. * @param location
  291. */
  292. public ShowInterstitial(location: BannerLocation = null) {
  293. if (PlatUtils.IsQQ) {
  294. if (!utils.Tool_QQ.isOverMinVersion("1.12.0")) {
  295. utils.showLog("当前版本" + utils.Tool_QQ._sysInfo.SDKVersion + "不支持插屏广告和盒子广告!");
  296. return;
  297. }
  298. if (this.ServerConfig) {
  299. if (this.ServerConfig.intersititial_first_ad) {
  300. utils.showLog(`服务器插屏优先级>>>${this.ServerConfig.intersititial_first_ad}`);
  301. switch (this.ServerConfig.intersititial_first_ad) {
  302. case "default":
  303. utils.delayCall(this._createInsterstitial.bind(this), this.ServerConfig.intersititia_delay_show_time || 0);
  304. break;
  305. case "box":
  306. if (!utils.Tool_QQ.isOverMinVersion("1.7.1")) {
  307. utils.showLog("当前版本" + utils.Tool_QQ._sysInfo.SDKVersion + "不支持盒子广告,显示插屏");
  308. utils.delayCall(this._createInsterstitial.bind(this), this.ServerConfig.intersititia_delay_show_time || 0);
  309. return;
  310. }
  311. utils.delayCall(this.ShowAppBox.bind(this), this.ServerConfig.intersititia_delay_show_time || 0);
  312. break;
  313. case "onlydefault":
  314. utils.delayCall(this._createInsterstitial.bind(this), this.ServerConfig.intersititia_delay_show_time || 0);
  315. break;
  316. }
  317. } else {
  318. utils.showLog("服务器未配置优先级,显示默认插屏");
  319. utils.delayCall(this._createInsterstitial.bind(this), this.ServerConfig.intersititia_delay_show_time || 0);
  320. }
  321. }
  322. }
  323. }
  324. _isShow: boolean = false;
  325. public _createInsterstitial() {
  326. utils.showLog("_createInsterstitial >>> 222");
  327. // if (this._insertAd) {
  328. // this._insertAd.destroy();
  329. // }
  330. // let insertId;
  331. if (!this._insertAd) {
  332. this._insertAd = this.qq.createInterstitialAd({ adUnitId: utils.config.qqconfig.insertId })
  333. this._insertAd.onError((err) => {
  334. switch (err.errCode) {
  335. case 2001:
  336. utils.showLog("小程序启动一定时间内不允许展示插屏广告");
  337. break;
  338. case 2002:
  339. utils.showLog("距离小程序插屏广告或者激励视频广告上次播放时间间隔不足,不允许展示插屏广告");
  340. break;
  341. case 2003:
  342. utils.showLog("当前正在播放激励视频广告或者插屏广告,不允许再次展示插屏广告");
  343. break;
  344. case 2004:
  345. utils.showLog("该项错误不是开发者的异常情况,或因小程序页面切换导致广告渲染失败");
  346. break;
  347. case 2005:
  348. utils.showLog("插屏广告实例不允许跨页面调用");
  349. break;
  350. default:
  351. utils.showLog("插屏加载失败! ", JSON.stringify(err));
  352. break;
  353. }
  354. if (this.ServerConfig.intersititial_first_ad && this.ServerConfig.intersititial_first_ad == "default") {
  355. utils.showLog("优先显示的是插屏广告,备用显示盒子广告!");
  356. let show_level = this.ServerConfig.insert_show_appBox_level ? this.ServerConfig.insert_show_appBox_level : 0;
  357. if (utils.currentLevel > show_level) {
  358. this.ShowAppBox();
  359. } else {
  360. utils.showLog("备用显示盒子广告未达到关卡限制要求");
  361. }
  362. }
  363. });
  364. // this._insertAd.onLoad(() => {
  365. // utils.showLog("插屏加载成功! ");
  366. // })
  367. this._insertAd.onClose(() => {
  368. utils.showLog("插屏广告关闭成功!");
  369. });
  370. }
  371. if (this._insertAd) {
  372. this._insertAd.load().then(() => {
  373. utils.showLog("插屏加载成功! ");
  374. this._insertAd.show().then(() => {
  375. utils.showLog("插屏广告显示成功!");
  376. }).catch((err) => {
  377. utils.showLog("插屏广告出错", JSON.stringify(err));
  378. });
  379. }).catch(() => {
  380. utils.showLog("插屏加载失败! ");
  381. });
  382. if (!this._isShow) {
  383. this._insertAd.show().then(() => {
  384. utils.showLog("插屏广告显示成功!");
  385. }).catch((err) => {
  386. utils.showLog("插屏广告出错", JSON.stringify(err));
  387. });
  388. this._isShow = true;
  389. }
  390. }
  391. }
  392. /**
  393. * 显示游戏盒子
  394. */
  395. public ShowAppBox(isMoreGame?: boolean) {
  396. if (PlatUtils.IsQQ) {
  397. if (!utils.Tool_QQ.isOverMinVersion("1.7.1")) {
  398. utils.showLog("当前版本" + utils.Tool_QQ._sysInfo.SDKVersion + "不支持游戏盒子!");
  399. return;
  400. }
  401. utils.showLog(">>>>>ShowAppBox");
  402. if (this._appBox) {
  403. this._appBox.destroy().then(() => {
  404. utils.showLog("游戏盒子id为:", utils.config.qqconfig.boxId);
  405. this._appBox = this.qq.createAppBox({
  406. adUnitId: utils.config.qqconfig.boxId
  407. });
  408. this._appBox.load().then(() => {
  409. utils.showLog("游戏盒子加载成功!")
  410. this._appBox.show().then(() => {
  411. utils.showLog("游戏盒子显示成功!");
  412. }).catch((err) => {
  413. utils.showLog("游戏盒子未加载!#err=", JSON.stringify(err));
  414. });
  415. }).catch((err) => {
  416. utils.showLog("游戏盒子加载失败! #err=", JSON.stringify(err));
  417. if (!isMoreGame && this.ServerConfig.intersititial_first_ad && this.ServerConfig.intersititial_first_ad == "box") {
  418. utils.showLog("优先显示的是盒子广告,备用显示插屏");
  419. this._createInsterstitial();
  420. }
  421. });
  422. }).catch((err) => {
  423. utils.showLog("游戏盒子销毁失败! #err=", JSON.stringify(err));
  424. });;
  425. } else {
  426. utils.showLog("游戏盒子id为:", utils.config.qqconfig.boxId);
  427. this._appBox = this.qq.createAppBox({
  428. adUnitId: utils.config.qqconfig.boxId
  429. });
  430. this._appBox.load().then(() => {
  431. utils.showLog("游戏盒子加载成功!")
  432. this._appBox.show().then(() => {
  433. utils.showLog("游戏盒子显示成功!");
  434. }).catch((err) => {
  435. utils.showLog("游戏盒子未加载!#err=", JSON.stringify(err));
  436. });
  437. }).catch((err) => {
  438. utils.showLog("游戏盒子加载失败! #err=", JSON.stringify(err));
  439. if (!isMoreGame && this.ServerConfig.intersititial_first_ad && this.ServerConfig.intersititial_first_ad == "box") {
  440. utils.showLog("优先显示的是盒子广告,备用显示插屏");
  441. this._createInsterstitial();
  442. }
  443. });
  444. }
  445. }
  446. }
  447. /**
  448. * 隐藏游戏盒子
  449. */
  450. public HideAppbox() {
  451. if (PlatUtils.IsQQ) {
  452. if (this._appBox) {
  453. this._appBox.destroy().then(() => {
  454. utils.showLog("游戏盒子销毁成功!");
  455. }).catch(() => {
  456. cc.warn("游戏盒子销毁失败!");
  457. });
  458. }
  459. }
  460. }
  461. _blockAd: any = null;
  462. /**
  463. * 显示积木广告
  464. */
  465. public showBlockAd(parme?: any) {
  466. if (!this.ServerConfig) {
  467. utils.showLog("组件未初始化!");
  468. return;
  469. }
  470. if (!utils.config.qqconfig.bannerBoxId) {
  471. utils.showLog("积木广告ID不存在");
  472. return;
  473. }
  474. if (this.ServerConfig.is_banner_box == "false") {
  475. utils.showLog("服务器配置积木广告不展示!");
  476. return;
  477. }
  478. if (this._blockAd) {
  479. this._blockAd.offLoad();
  480. this._blockAd.offResize();
  481. this._blockAd.offError();
  482. this._blockAd.destroy();
  483. }
  484. if (PlatUtils.IsIOS && this._sysData.screenHeight < 736) {
  485. let top = 16;
  486. let left = 16;
  487. let width = 65;
  488. if (parme.top) {
  489. // top = parme.top
  490. top = parme.top / cc.winSize.height * this._sysData.screenHeight
  491. } else {
  492. // top = this._sysData.screenHeight - 65.5 - parme.bottom
  493. top = this._sysData.screenHeight - 65.5 - parme.bottom / cc.winSize.height * this._sysData.screenHeight;
  494. }
  495. this._blockAd = this.qq.createBlockAd({
  496. adUnitId: utils.config.qqconfig.bannerBoxId,
  497. style: {
  498. left: left,
  499. top: top
  500. },
  501. size: parme.showNum ? parme.showNum : 5,
  502. orientation: "landscape"
  503. })
  504. this._blockAd.onResize((res) => {
  505. this._blockAd.offResize();
  506. utils.showLog("this._sysData", JSON.stringify(this._sysData));
  507. utils.showLog("onresize >>", JSON.stringify(res));
  508. this._blockAd.show().then(() => {
  509. utils.showLog("显示成功!");
  510. }).catch((res) => {
  511. utils.showLog("显示异常", JSON.stringify(res));
  512. });
  513. if (parme.showNum == 1) {
  514. width = 0
  515. } else {
  516. width = res.width;
  517. }
  518. if (parme.left) {
  519. // left = width + parme.left;
  520. left = parme.left / cc.winSize.width * this._sysData.screenWidth;
  521. } else if (parme.right) {
  522. left = this._sysData.screenWidth - res.width - parme.right / cc.winSize.width * this._sysData.screenWidth;
  523. } else {
  524. left = (this._sysData.screenWidth - width) / 2;
  525. }
  526. this._blockAd.style.left = left;
  527. utils.showLog("onresize end>>", this._blockAd.style.top, "<<", this._blockAd.style.left, "<<");
  528. })
  529. } else {
  530. this._blockAd = this.qq.createBlockAd({
  531. adUnitId: utils.config.qqconfig.bannerBoxId,
  532. style: {
  533. left: 16,
  534. top: 16
  535. },
  536. size: parme.showNum ? parme.showNum : 5,
  537. orientation: "landscape"
  538. })
  539. // if (parme.top) {
  540. // console.log("实际位置:", parme.top / cc.winSize.height * this._sysData.screenHeight, cc.winSize.height, parme.top)
  541. // }
  542. // else if (parme.bottom) {
  543. // console.log("实际位置:", this._sysData.screenHeight - 60 - parme.bottom / cc.winSize.height * this._sysData.screenHeight,cc.winSize.height,parme.bottom)
  544. // }
  545. this._blockAd.onResize((res) => {
  546. this._blockAd.offResize();
  547. utils.showLog("this._sysData", JSON.stringify(this._sysData));
  548. utils.showLog("onresize >>", JSON.stringify(res));
  549. if (parme) {
  550. utils.showLog("parme:" + JSON.stringify(parme))
  551. if (parme.top) {
  552. utils.showLog("积木广告TOP位置:", parme.top);
  553. this._blockAd.style.top = parme.top / cc.winSize.height * this._sysData.screenHeight;
  554. } else if (parme.bottom) {
  555. // let bottom = this._sysData.screenHeight - res.height;
  556. // this._blockAd.style.top = bottom - parme.bottom;
  557. this._blockAd.style.top = this._sysData.screenHeight - res.height - parme.bottom / cc.winSize.height * this._sysData.screenHeight;
  558. }
  559. if (parme.left) {
  560. // this._blockAd.style.left = parme.left;
  561. this._blockAd.style.left = parme.left / cc.winSize.width * this._sysData.screenWidth;
  562. } else if (parme.right) {
  563. // this._blockAd.style.left = this._sysData.screenWidth - res.width - parme.right;
  564. this._blockAd.style.left = this._sysData.screenWidth - res.width - parme.right / cc.winSize.width * this._sysData.screenWidth;
  565. } else {
  566. this._blockAd.style.left = (this._sysData.screenWidth - res.width) / 2;
  567. }
  568. } else {
  569. this._blockAd.style.top = this._sysData.screenHeight - res.height;
  570. this._blockAd.style.left = (this._sysData.screenWidth - res.width) / 2;
  571. }
  572. this._blockAd.show().then(() => {
  573. utils.showLog("显示成功!");
  574. }).catch((res) => {
  575. utils.showLog("显示异常", JSON.stringify(res));
  576. });
  577. utils.showLog("onresize end>>", this._blockAd.style.top, "<<", this._blockAd.style.left, "<<");
  578. })
  579. }
  580. this._blockAd.onLoad(() => {
  581. utils.showLog("积木广告加载成功!");
  582. });
  583. this._blockAd.onError((res) => {
  584. utils.showLog("积木广告加载失败!", res.errMsg, ">>", res.errCode);
  585. });
  586. }
  587. /**
  588. * 隐藏积木广告
  589. */
  590. public hideBlockAd() {
  591. utils.showLog("隐藏积木广告");
  592. if (this._blockAd) this._blockAd.hide()
  593. }
  594. public moveBtnToBannerTop() {
  595. utils.showLog("moveBtnToBannerTop >>>>>>>>>.");
  596. if (this.ServerConfig && this._moveBtn && cc.isValid(this._moveBtn)) {
  597. let moveBtnTime = 0;
  598. let btn: cc.Node = this._moveBtn;
  599. if (this.ServerConfig.banner_move_btn_interval && this._cur_level && this._cur_level % this.ServerConfig.banner_move_btn_interval == 0) {
  600. if (this.ServerConfig.banner_move_btn_time) {
  601. moveBtnTime = this.ServerConfig.banner_move_btn_time;
  602. }
  603. }
  604. utils.showLog(moveBtnTime == 0 ? "按钮直接显示在banner上面" : `按钮居底部且${moveBtnTime}秒后移动`);
  605. setTimeout(() => {
  606. if (btn && cc.isValid(btn)) {
  607. if (this._sysData.screenHeight < 600) {
  608. btn.getComponent(cc.Widget).bottom = 250;
  609. } else {
  610. btn.getComponent(cc.Widget).bottom = 220;
  611. }
  612. btn.getComponent(cc.Widget).updateAlignment();
  613. }
  614. }, moveBtnTime * 1000);
  615. }
  616. }
  617. }