Hopefully this helps, these are the main pieces I have been working with.
Here is the adlistener:
--=================================Ad listener============================================ --This listener is for Ads function adListener( event ) local msg = event.response -- just a quick debug message to check what response we got from the library --print("Message received from the ads library: ", msg) if ( event.isError ) then -- attempt to fetch another ad print("---------------------ad error----------------------") elseif ( event.phase == "loaded" ) then -- an ad was preloaded print("---------------------ad is loaded and ready----------------------") DATA.adshown = false elseif ( event.phase == "shown" ) then -- the ad was viewed and closed print("---------------------ad shown----------------------") if ( event.type == "interstitial" ) then DATA.adshown = true – this is flag being watched end end end
And this is the function that moves from the gamescreen to the adscreen or scorescreen depending on a count, if the ad is loaded, game modes, if the adfree option was purchased, etc. It also loads the ad when appropriate. It is a function called in the gamescreen.
--This function is for redirect user to score screen goToScoreScreen = function() audio.stop(gameBgChannel) adsLib:hideAd() for i=1,5 do if ansText[i].text == "" or tonumber(ansText[i].text) == nil then DATA.userAns[i] = "-" else DATA.userAns[i] = tonumber(ansText[i].text) end end local options = { effect = "fromRight", time = 800, params ={screen="playerDataScreen"} } -- logic to go to the adScene or to the scoreScene -- check if ad is to be displayed if DATA.adPurchased == false and DATA.isRainbow == false then if DATA.displayAds == true then if DATA.counterforad == 2 then -- number of rounds to skip before displaying ad, 2 for testing if ( adsLib:isLoaded("interstitial") ) then -- check if the ad is loaded, go to adScene if ad is ready composer.gotoScene("adScreen", options ) else adsLib:loadAd( "interstitial" ) -- this is the ads.loadAd call composer.gotoScene("scoreScreen", options ) -- continue to score end else DATA.counterforad = DATA.counterforad + 1 -- count the round, go to score as normal composer.gotoScene("scoreScreen", options ) end end elseif DATA.isRainbow == true then composer.gotoScene("scoreScreen", options ) end end
And here is the adscreen which shows the ad and then show the scorescreen
gotoscoreScreen = function () composer.gotoScene("scoreScreen", options ) end timerpause = function() gotoscoreScreen() end adshowncheck = function(event) if DATA.adshown == true then DATA.adshown = false --adsLib:hideAd() timer.cancel(adtimer) composer.gotoScene("scoreScreen", options ) composer.removeScene( "adScreen" ) -- timer.cancel(adtimer) end end -- ------------------------------------------------------------------------------- -- "scene:create()" function scene:create( event ) local group = self.view -- Initialize the scene here. -- Example: add display objects to "sceneGroup", add touch listeners, etc. --print ("wait for ad to close") adsLib:showAd( "interstitial", nil ) -- this calls the ads.showAd function in adsLib DATA.counterforad = 1 -- reset counter DATA.adcalled = true --loop until ad is set as 'shown' adtimer = timer.performWithDelay(200,adshowncheck,0) --timer.performWithDelay(3000,timerpause) --gotoscoreScreen() end -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is still off screen (but is about to come on screen). elseif ( phase == "did" ) then -- Called when the scene is now on screen. -- Insert code here to make the scene come alive. -- Example: start timers, begin animation, play audio, etc. composer.removeHidden() end end