Hi. why does the ad show after the scene appears? And how to fix it?
Here is my code.
local function handleButtonEvent( event )
if ( "ended" == event.phase ) then
ads:showInterstitial()
composer.gotoScene("answer")
end
end
Ads are preloaded. here is my ads.lua code
local admob = require( "plugin.admob" )
local ads = {}
ads.adListener = function(event)
if ( event.phase == "init" ) then
admob.load( "interstitial", { adUnitId="ca-app-pub-3940256099942544/1033173712" } )
end
if ( event.phase == "displayed" ) then
if ( admob.isLoaded( "interstitial" ) == false ) then
admob.load( "interstitial", { adUnitId="ca-app-pub-3940256099942544/1033173712" } )
end
end
if ( event.phase == "closed") then
composer.gotoScene("answer")
end
end
function ads:init()
admob.init( ads.adListener, { testMode = true } )
end
function ads:isL()
return admob.isLoaded( "interstitial" )
end
function ads:loadInterstitial()
admob.load( "interstitial", { adUnitId="ca-app-pub-3940256099942544/1033173712" } )
end
function ads:showInterstitial()
admob.show("interstitial")
end
return ads
You’ve got composer.gotoScene("answer") firing immediately after you call ads:showInterstitial() in your handleButtonEvent function and again as a condition of event.phase == "closed" in the listener.
My guess is that the gotoScene is occurring before the application suspends to show the ad, making your command in the listener redundant. Try commenting out the gotoScene in the button event and see if that fixes it.
Its a good practice to insert “cutscene” where ads show between your main scene and “answer”. You can also try to put a delay to show the scene
Step one is making sure that the event is firing. I’d put a print statement in the listener like so:
if ( event.phase == "closed") then
print ( "Trying to go to the answer scene!" )
composer.gotoScene("answer")
end
I have the sneaking suspicion that you’ve got your listener set up incorrectly and it’s not actually doing anything. If the print statement doesn’t show up, I’d try setting up your listener like so: local function adListener( event )
and then your init call like this: admob.init( adListener, { testMode = true } )
You only need to predicate functions with the module name when they are going to be called externally. Your listener is being called internally, so it should be a local function.
Hello.
Thanks for the help. I made corrections (if I understand correctly).this code works on one scene
local composer = require( "composer" )
local scene = composer.newScene();
local widget = require( "widget" )
local admob = require( "plugin.admob" )
local button1
local function adListener( event )
if ( event.phase == "init" ) then -- Successful initialization
-- Load an AdMob interstitial ad
admob.load( "interstitial", { adUnitId="ca-app-pub-3940256099942544/1033173712" } )
end
if ( event.phase == "displayed" ) then
if ( admob.isLoaded( "interstitial" ) == false ) then
admob.load( "interstitial", { adUnitId="ca-app-pub-3940256099942544/1033173712" } )
end
end
if ( event.phase == "closed") then
composer.gotoScene("menu")
end
end
local function init()
admob.init( adListener, { testMode = true } )
end
local function isL()
return admob.isLoaded( "interstitial" )
end
local function loadInterstitial()
admob.load( "interstitial", { adUnitId="ca-app-pub-3940256099942544/1033173712" } )
end
local function showInterstitial()
admob.show("interstitial")
end
local function handleButtonEvent( event )
if ( "ended" == event.phase ) then
showInterstitial()
end
end
---------------------------------------------------------------
function scene:create( event )
local sceneGroup = self.view
init()
But
when I use the listener module it doesn’t work. The ad loads and shows but doesn’t move to another scene.
my main code in ads.lua file. init i call in main.lua file. I show ads in the game.lua file
Sorry, I’m still confused as to what’s going on in your code. Now it looks like you’re putting the button the the AdMob module? I understand that you’re not posting all your code so that things don’t get too confusing, but you’re leaving out important bits. For example, you’re not showing the part of the listener that is supposed to be launching the new scene. This is a pretty critical part of the code when it comes to diagnosing the problem you’re having changing scenes. You’ve also changed how the listener is set up, but you don’t show the init call, so that we can see if the listener is being referenced correctly.
I’d say that step one would be to take another look at the sample code in the docs and set things up to mirror them as closely as possible. After you’ve got the listener configured so that it’s working as expected, branch out into solving the problem of having the listener trigger the scene change.