Hello everyone Perhaps this has already been discussed somewhere, I searched, but did not find enough information. It is scattered somewhere in pieces. I have a vague picture on this issue. I would like someone to just clarify the situation(helped me put the puzzle together):
In order to display the reward and interstitial ads, do I have to download them in advance? And if in advance, where? In the main file, or another one? I would really like to just see: which code is in main, which is in scene.
To test the ad, I have to set testmod=true. But where? I would be extremely grateful if someone would show examples with the code. Thank you in advance!
What about rewarded ads? I understand that it is necessary to produce the “init” stage in “main”, and the “isLoaded” and "show"phase directly on the stage. As I understand it, ads should always be loaded in advance, but in my app, all the phases go on a certain scene. Should I fix this?
And does it work for both interstitial and rewards, or are there certain features?
It isn’t necessary to initialise the plugin in main.lua. In fact, I would recommend that you create a separate “ads.lua” module that contains all AdMob related code.
You can require this ads module from main and start the initialisation as soon as the app starts, but you would also be able to require the module in other scenes and handle function callbacks easier this way.
admob.init() is an asynchronous function. This means that it may take your app some time before the function call completes. The app will, however, keep on executing your code.
You need to wait for admob to be initialised before you can try showing ads.
Hi. Thanks a lot. I wouldn’t have thought of it. I added a button and pressed it for a bit. The ad showed.
But there is a new problem: When I close the ad and press the button again, the ad is no longer shown
My code
local widget = require( "widget" )
local admob = require( "plugin.admob" )
local Mytext = display.newText( "start", display.contentCenterX, 100,"OpenSans-
Bold.ttf",16)
local function handleButtonEvent( event )
if ( "ended" == event.phase ) then
-- Sometime later, show the interstitial ad
if ( admob.isLoaded( "interstitial" ) ) then
Mytext.text = "isLoaded"
admob.show( "interstitial" )
else
Mytext.text = "dont isLoaded"
end
end
end
local button1 = widget.newButton(
{
left = 100,
top = 200,
id = "button1",
label = "Default",
onEvent = handleButtonEvent
}
)
-- AdMob listener function
local function adListener( event )
if ( event.phase == "init" ) then -- Successful initialization
-- Load an AdMob interstitial ad
Mytext.text = "init"
admob.load( "interstitial", { adUnitId="ca-app-pub-3940256099942544/1033173712" } )
else
Mytext.text = "Dont init"
end
end
-- Initialize the AdMob plugin
admob.init( adListener, { testMode = true } )
You need to require the AdMob plugin any time you want to be able to call it.
If you plan on using AdMob (or anything else) in multiple scenes, you might want to consider writing a module for AdMob, and then just requiring that. It will save you from having to duplicate your listener and everything else in every scene you where want to have access to AdMob.