ads.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. var videoContent = document.getElementById('contentElement');
  2. var adDisplayContainer = new google.ima.AdDisplayContainer(document.getElementById('adContainer'), videoContent);
  3. // Must be done as the result of a user action on mobile
  4. adDisplayContainer.initialize();
  5. // Re-use this AdsLoader instance for the entire lifecycle of your page.
  6. var adsLoader = new google.ima.AdsLoader(adDisplayContainer);
  7. // Add event listeners
  8. adsLoader.addEventListener(google.ima.AdsManagerLoadedEvent.Type.ADS_MANAGER_LOADED, onAdsManagerLoaded, false);
  9. adsLoader.addEventListener(google.ima.AdErrorEvent.Type.AD_ERROR, onAdError, false);
  10. function onAdError(adErrorEvent) {
  11. console.log("adError");
  12. document.getElementById("adContainer").style.display = "none";
  13. var GameConfig = __require("GameConfig");
  14. if (GameConfig.playNum == 0) {
  15. adCompleteFlag = true;
  16. if (adCompleteFlag && resCompleteFlag) {
  17. adCompleteFlag = false;
  18. var launchScene = GameConfig.launchScene;
  19. var Bros = GameConfig.Bros;
  20. var caS = GameConfig.caS;
  21. cc.director.loadScene(launchScene, null,
  22. function () {
  23. adCompleteFlag = false;
  24. if (Bros) {
  25. // show canvas
  26. caS = '';
  27. var div = document.getElementById('GameDiv');
  28. if (div) {
  29. div.style.backgroundImage = '';
  30. }
  31. }
  32. cc.loader.onProgress = null;
  33. console.log('Success to load scene: ' + launchScene);
  34. }
  35. );
  36. }
  37. }
  38. adEndComplete = true;
  39. if (adEndComplete && resEndComplete) {
  40. adEndComplete = false;
  41. console.log("indexOverErr");
  42. var MainManger = __require("MainManage");
  43. MainManger.showGameEndLayer();
  44. }
  45. // Handle the error logging and destroy the AdsManager
  46. console.log(adErrorEvent.getError());
  47. adsManager.destroy();
  48. }
  49. // An event listener to tell the SDK that our content video
  50. // is completed so the SDK can play any post-roll ads.
  51. var contentEndedListener = function () {
  52. adsLoader.contentComplete();
  53. };
  54. videoContent.onended = contentEndedListener;
  55. // Request video ads.
  56. var preloader = new google.ima.AdsRequest();
  57. preloader.adTagUrl = 'https://googleads.g.doubleclick.net/pagead/ads?ad_type=video_image&client=ca-games-pub-8708616103041212&description_url=http%3A%2F%2Fwww.vsane.com&videoad_start_delay=-1&hl=zh_CN&max_ad_duration=15000';
  58. // Specify the linear and nonlinear slot sizes. This helps the SDK to
  59. // select the correct creative if multiple are returned.
  60. preloader.linearAdSlotWidth = 640;
  61. preloader.linearAdSlotHeight = 400;
  62. preloader.nonLinearAdSlotWidth = 640;
  63. preloader.nonLinearAdSlotHeight = 400;
  64. // showMyAds();
  65. function showMyAds() {
  66. // alert("showMy");
  67. if (typeof (killads) == 'undefined') {
  68. // alert('广告被过滤');
  69. console.log("AdNo");
  70. adEndComplete = false;
  71. var MainManger = __require("MainManage");
  72. MainManger.showGameEndLayer();
  73. } else {
  74. var winHeight = document.documentElement.clientHeight;
  75. // document.getElementById("adContainer").style.height = winHeight-60+"px";
  76. if (document.body.clientHeight > 700) {
  77. document.getElementById("adContainer").style.height = winHeight - 85 + "px";
  78. } else {
  79. document.getElementById("adContainer").style.height = winHeight - 65 + "px";
  80. }
  81. document.getElementById("adContainer").style.display = "block";
  82. adsLoader.requestAds(preloader);
  83. }
  84. }
  85. function onAdsManagerLoaded(adsManagerLoadedEvent) {
  86. console.log("ADLoad");
  87. adCompleteFlag = true;
  88. // document.getElementById("adContainer").style.display = "block";
  89. // Get the ads manager.
  90. adsManager = adsManagerLoadedEvent.getAdsManager(videoContent); // See API reference for contentPlayback
  91. // Add listeners to the required events.
  92. adsManager.addEventListener(google.ima.AdErrorEvent.Type.AD_ERROR, onAdError);
  93. adsManager.addEventListener(google.ima.AdEvent.Type.CONTENT_PAUSE_REQUESTED, onContentPauseRequested);
  94. adsManager.addEventListener(google.ima.AdEvent.Type.CONTENT_RESUME_REQUESTED, onContentResumeRequested);
  95. var o = document.getElementById("adContainer");
  96. var h = o.offsetHeight; //高度
  97. var w = o.offsetWidth; //宽度
  98. try {
  99. // Initialize the ads manager. Ad rules playlist will start at this time.
  100. adsManager.init(w, h, google.ima.ViewMode.FULLSCREEN);
  101. // Call start to show ads. Single video and overlay ads will
  102. // start at this time; this call will be ignored for ad rules, as ad rules
  103. // ads start when the adsManager is initialized.
  104. adsManager.start();
  105. } catch (adError) {
  106. // An error may be thrown if there was a problem with the VAST response.
  107. // Play content here, because we won't be getting an ad.
  108. // videoContent.play();
  109. }
  110. }
  111. function onContentPauseRequested() {
  112. // This function is where you should setup UI for showing ads (e.g.
  113. // display ad timer countdown, disable seeking, etc.)
  114. videoContent.removeEventListener('ended', contentEndedListener);
  115. videoContent.pause();
  116. }
  117. // 广告结束的时候调用的函数
  118. function onContentResumeRequested() {
  119. console.log("ADLoadComplete", resEndComplete);
  120. var GameConfig = __require("GameConfig");
  121. console.log("IndexMainManger", GameConfig.launchScene, GameConfig.Bros, GameConfig.caS);
  122. if (GameConfig.playNum == 1) {
  123. adCompleteFlag = true;
  124. if (adCompleteFlag && resCompleteFlag) {
  125. var launchScene = GameConfig.launchScene;
  126. var Bros = GameConfig.Bros;
  127. var caS = GameConfig.caS;
  128. cc.director.loadScene(launchScene, null,
  129. function () {
  130. adCompleteFlag = false;
  131. if (Bros) {
  132. // show canvas
  133. var canvas = document.getElementById('GameCanvas');
  134. canvas.style.visibility = '';
  135. var div = document.getElementById('GameDiv');
  136. if (div) {
  137. div.style.backgroundImage = '';
  138. }
  139. }
  140. cc.loader.onProgress = null;
  141. console.log('Success to load scene1: ' + launchScene);
  142. }
  143. );
  144. }
  145. }
  146. adEndComplete = true;
  147. if (adEndComplete && resEndComplete) {
  148. adEndComplete = false;
  149. console.log("indexOverErr");
  150. var MainManger = __require("MainManage");
  151. MainManger.showGameEndLayer();
  152. }
  153. // This function is where you should ensure that your UI is ready
  154. // to play content.
  155. document.getElementById("adContainer").style.display = "none";
  156. // videoContent.addEventListener('ended', contentEndedListener);
  157. // videoContent.play();
  158. }
  159. function noAdGoToScene() {
  160. var GameConfig = __require("GameConfig");
  161. console.log("IndexMainMangerMaing", GameConfig.launchScene, GameConfig.Bros, GameConfig.caS);
  162. var launchScene = GameConfig.launchScene;
  163. var Bros = GameConfig.Bros;
  164. var caS = GameConfig.caS;
  165. cc.director.loadScene(launchScene, null,
  166. function () {
  167. adCompleteFlag = false;
  168. if (Bros) {
  169. // show canvas
  170. var canvas = document.getElementById('GameCanvas');
  171. canvas.style.visibility = '';
  172. var div = document.getElementById('GameDiv');
  173. if (div) {
  174. div.style.backgroundImage = '';
  175. }
  176. }
  177. cc.loader.onProgress = null;
  178. console.log('Success to load scene1Main: ' + launchScene);
  179. }
  180. );
  181. }