hi guys in my journey to test ads I’m trying to make an external module to implement ads. Here is the code:
--ads.lua local M = {} local appodeal = require( "plugin.appodeal" ) -- Ad listener function M.adsListener = function(event) -- Successful initialization of the Appodeal plugin if ( event.phase == "init" ) then print( "Appodeal event: initialization successful" ) -- An ad loaded successfully elseif ( event.phase == "loaded" ) then print( "Appodeal event: ad loaded successfully" ) appodeal.load("interstitial") appodeal.load("rewardedVideo") -- The ad was displayed/played elseif ( event.phase == "displayed" or event.phase == "playbackBegan" ) then print( "Appodeal event: ad displayed" ) -- The ad was closed/hidden/completed elseif ( event.phase == "hidden" or event.phase == "closed" or event.phase == "playbackEnded" ) then print( "Appodeal event: ad closed/hidden/completed" ) -- The user clicked/tapped an ad elseif ( event.phase == "clicked" ) then print( "Appodeal event: ad clicked/tapped" ) -- The ad failed to load elseif ( event.phase == "failed" ) then print( "Appodeal event: ad failed to load" ) end end M.startAdsPlugin = function(event) appodeal.init( M.adsListener, { appKey = "xxxxxxxxxxxxx", testMode = true, childDirectedTreatment = true, disableAutoCacheForAdTypes = { "interstitial", "rewardedVideo" }, disableWriteExternalPermissionCheck = true, supportedAdTypes = {"interstitial", "rewardedVideo"} } ) end M.showInterstitialAd = function(event) print( "--Start to show interstitial ad--" ) local adTimer = nil local adShowAttempts = 10 local function showAd() if adTimer then timer.cancel( adTimer ) end adShowAttempts = adShowAttempts - 1 if adShowAttempts \<= 0 then return end if not appodeal.isLoaded( "interstitial" ) then adTimer = timer.performWithDelay( 500, showAd ) else appodeal.show( "interstitial" ) end adTimer = timer.performWithDelay( 100, showAd ) end end return M
in main lua I require ads.lua and initialize the appodeal plugin
--main.lua local ads = require( "ads" ) ads.startAdsPlugin()
and in level 1 in the scene:show did phase I call
M.showInterstitialAd() but no ad show
-- show() function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is still off screen (but is about to come on screen) elseif ( phase == "did" ) then -- Code here runs when the scene is entirely on screen --test ADS\*\*\*\*\* ads.showInterstitialAd() --ad not show end end
I just want to understand how to show an ad for future use in all scenes
Thanks in advance
DoDi