-
Notifications
You must be signed in to change notification settings - Fork 1
/
ads.js
290 lines (197 loc) · 8.2 KB
/
ads.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
var videoContent = document.getElementById('contentElement');
var adDisplayContainer =
new google.ima.AdDisplayContainer(
document.getElementById('adContainer'),
videoContent);
// Must be done as the result of a user action on mobile
adDisplayContainer.initialize();
// Re-use this AdsLoader instance for the entire lifecycle of your page.
var adsLoader = new google.ima.AdsLoader(adDisplayContainer);
// Add event listeners
adsLoader.addEventListener(
google.ima.AdsManagerLoadedEvent.Type.ADS_MANAGER_LOADED,
onAdsManagerLoaded,
false);
adsLoader.addEventListener(
google.ima.AdErrorEvent.Type.AD_ERROR,
onAdError,
false);
function onAdError(adErrorEvent) {
console.log("adError");
document.getElementById("adContainer").style.display = "none";
var GameConfig = __require("GameConfig");
if (GameConfig.playNum == 0) {
adCompleteFlag = true;
if (adCompleteFlag && resCompleteFlag) {
adCompleteFlag = false;
var launchScene = GameConfig.launchScene;
var Bros = GameConfig.Bros;
var caS = GameConfig.caS;
cc.director.loadScene(launchScene, null,
function() {
adCompleteFlag = false;
if (Bros) {
// show canvas
caS = '';
var div = document.getElementById('GameDiv');
if (div) {
div.style.backgroundImage = '';
}
}
cc.loader.onProgress = null;
console.log('Success to load scene: ' + launchScene);
}
);
}
}
adEndComplete = true;
if (adEndComplete && resEndComplete) {
adEndComplete = false;
console.log("indexOverErr");
var MainManger = __require("MainManage");
MainManger.showGameEndLayer();
}
// Handle the error logging and destroy the AdsManager
console.log(adErrorEvent.getError());
adsManager.destroy();
}
// An event listener to tell the SDK that our content video
// is completed so the SDK can play any post-roll ads.
var contentEndedListener = function() {
adsLoader.contentComplete();
};
videoContent.onended = contentEndedListener;
// Request video ads.
var preloader = new google.ima.AdsRequest();
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';
// Specify the linear and nonlinear slot sizes. This helps the SDK to
// select the correct creative if multiple are returned.
preloader.linearAdSlotWidth = 640;
preloader.linearAdSlotHeight = 400;
preloader.nonLinearAdSlotWidth = 640;
preloader.nonLinearAdSlotHeight = 400;
// showMyAds();
function showMyAds() {
// alert("showMy");
if (typeof(killads) == 'undefined') {
// alert('广告被过滤');
console.log("AdNo");
adEndComplete = false;
var MainManger = __require("MainManage");
MainManger.showGameEndLayer();
} else {
var winHeight = document.documentElement.clientHeight;
// document.getElementById("adContainer").style.height = winHeight-60+"px";
if (document.body.clientHeight > 700) {
document.getElementById("adContainer").style.height = winHeight - 85 + "px";
} else {
document.getElementById("adContainer").style.height = winHeight - 65 + "px";
}
document.getElementById("adContainer").style.display = "block";
adsLoader.requestAds(preloader);
}
}
function onAdsManagerLoaded(adsManagerLoadedEvent) {
console.log("ADLoad");
adCompleteFlag = true;
// document.getElementById("adContainer").style.display = "block";
// Get the ads manager.
adsManager = adsManagerLoadedEvent.getAdsManager(
videoContent); // See API reference for contentPlayback
// Add listeners to the required events.
adsManager.addEventListener(
google.ima.AdErrorEvent.Type.AD_ERROR,
onAdError);
adsManager.addEventListener(
google.ima.AdEvent.Type.CONTENT_PAUSE_REQUESTED,
onContentPauseRequested);
adsManager.addEventListener(
google.ima.AdEvent.Type.CONTENT_RESUME_REQUESTED,
onContentResumeRequested);
var o = document.getElementById("adContainer");
var h = o.offsetHeight; //高度
var w = o.offsetWidth; //宽度
try {
// Initialize the ads manager. Ad rules playlist will start at this time.
adsManager.init(w, h, google.ima.ViewMode.FULLSCREEN);
// Call start to show ads. Single video and overlay ads will
// start at this time; this call will be ignored for ad rules, as ad rules
// ads start when the adsManager is initialized.
adsManager.start();
} catch (adError) {
// An error may be thrown if there was a problem with the VAST response.
// Play content here, because we won't be getting an ad.
// videoContent.play();
}
}
function onContentPauseRequested() {
// This function is where you should setup UI for showing ads (e.g.
// display ad timer countdown, disable seeking, etc.)
videoContent.removeEventListener('ended', contentEndedListener);
videoContent.pause();
}
//广告结束的时候调用的函数
function onContentResumeRequested() {
console.log("ADLoadComplete", resEndComplete);
var GameConfig = __require("GameConfig");
console.log("IndexMainManger", GameConfig.launchScene, GameConfig.Bros, GameConfig.caS);
if (GameConfig.playNum == 1) {
adCompleteFlag = true;
if (adCompleteFlag && resCompleteFlag) {
var launchScene = GameConfig.launchScene;
var Bros = GameConfig.Bros;
var caS = GameConfig.caS;
cc.director.loadScene(launchScene, null,
function() {
adCompleteFlag = false;
if (Bros) {
// show canvas
var canvas = document.getElementById('GameCanvas');
canvas.style.visibility = '';
var div = document.getElementById('GameDiv');
if (div) {
div.style.backgroundImage = '';
}
}
cc.loader.onProgress = null;
console.log('Success to load scene1: ' + launchScene);
}
);
}
}
adEndComplete = true;
if (adEndComplete && resEndComplete) {
adEndComplete = false;
console.log("indexOverErr");
var MainManger = __require("MainManage");
MainManger.showGameEndLayer();
}
// This function is where you should ensure that your UI is ready
// to play content.
document.getElementById("adContainer").style.display = "none";
// videoContent.addEventListener('ended', contentEndedListener);
// videoContent.play();
}
function noAdGoToScene() {
var GameConfig = __require("GameConfig");
console.log("IndexMainMangerMaing", GameConfig.launchScene, GameConfig.Bros, GameConfig.caS);
var launchScene = GameConfig.launchScene;
var Bros = GameConfig.Bros;
var caS = GameConfig.caS;
cc.director.loadScene(launchScene, null,
function() {
adCompleteFlag = false;
if (Bros) {
// show canvas
var canvas = document.getElementById('GameCanvas');
canvas.style.visibility = '';
var div = document.getElementById('GameDiv');
if (div) {
div.style.backgroundImage = '';
}
}
cc.loader.onProgress = null;
console.log('Success to load scene1Main: ' + launchScene);
}
);
}