我实现了Facebook Interstitial,并且在我的JS游戏中添加了RewardedVideo。我首先将广告加载到 document.ready
中$(document).ready(function() {
//Some game logic
FBInstant.initializeAsync().then(function() {
FBInstant.startGameAsync().then(function() {
var supportedAPIs = FBInstant.getSupportedAPIs();
if (supportedAPIs.includes('getInterstitialAdAsync') && supportedAPIs.includes('getRewardedVideoAsync')) {
//
// Preload Interstitial
//
FBInstant.getInterstitialAdAsync(
INTERSTITIAL_PLACEMENT_ID // Your Ad Placement Id
)
.then(function(interstitial) {
// Load the Ad asynchronously
preloadedInterstitial = interstitial;
return preloadedInterstitial.loadAsync();
})
.then(function() {})
.catch(function(err) {
displayError('Interstitial failed to preload: ' + err.message);
});
//
// Preload Rewarded
//
FBInstant.getRewardedVideoAsync(
REWARDED_PLACEMENT_ID // Your Ad Placement Id
)
.then(function(rewarded) {
// Load the Ad asynchronously
preloadedRewardedVideo = rewarded;
return preloadedRewardedVideo.loadAsync();
})
.then(function() {})
.catch(function(err) {
displayError('Rewarded video failed to preload:' + err.message);
});
} else {
displayError('Ads not supported in this session');
}
});
});
});
当我需要展示广告时,请调用以下方法
function showInterstitial() {
preloadedInterstitial
.showAsync()
.then(function() {
// Perform post-ad success operation
console.warn('Interstitial ad finished successfully');
})
.catch(function(e) {
console.error(e.message);
});
preloadedInterstitial.loadAsync(); <-- not sure
}
如您所见,每次需要新广告时,我都可以 preloadedInterstitial.loadAsync(); ,但我认为这会导致游戏运行缓慢。
与奖励视频相同
function showRewardedVideo() {
preloadedRewardedVideo
.showAsync()
.then(function() {
life++;
showSplash();
})
.catch(function(e) {
displayError(e.message);
});
preloadedRewardedVideo.loadAsync();
}
有什么建议吗?