Lua is a single pass compiler. When you call:
inMobi.init( adListener, { accountId="1527058984044" } )
What is the value of adListener? You haven’t defined it yet, so it’s nil. Your adListener function will never get called.
Next, I don’t believe this is correct:
bannerPlacementID = "[1525868581506]" interstitialPlacementID = "[79e0d9d0710a46b5846aee1929a6beac]"
The square brackets seem out of place. Try it with out them.
Since your adListener function is never called, you never load an ad. On top of that the code
if ( inMobi.isLoaded( bannerPlacementID ) ) then -- Show the ad inMobi.show( bannerPlacementID, { yAlign="bottom" } ) end
is likely going to execute before the .init() function completes. You need to understand a major concept in app development and that’s synchronous vs asynchronous actions. Synchronous actions happen in order. These activities happen quickly and don’t create pauses in the user’s experience. Somethings take time to complete like initializing an ad plugin, or logging in to a social media network. You don’t want the user having to wait on these activities to complete. So they happen in the background and notify you when they are complete. This is why there is an adListener function.
But since the .init() function fires in the background and notifies you when complete, code immediately after it executes as fast as it possibly can, so your inmobi.show() block will actually happen before the plugin has been initialized.
If you just want to build an ad demo app and you’re never going to have more than a main.lua and you always want a banner ad, then move the adListener function before you call inMobi.init(). You want to change your adListener function to something like:
local function adListener( event ) print( json.prettify( event ) ) if ( event.phase == "init" ) then -- Successful initialization -- Load a banner ad inMobi.load( "banner", bannerPlacementID ) elseif ( event.phase == "loaded" ) then -- Ad loaded successfully inMobi.show( bannerPlacementID, { yAlign="bottom" } ) elseif ( event.phase == "failed" ) then -- The ad failed to load print( event.type ) print( event.bannerPlacementID ) print( event.isError ) print( event.response ) end end
But once you’ve moved beyond a basic app all in main.lua, you’re going to have to learn how to call your show function when you’re in the scene you want, hide it when you don’t want to show it, and all of that rarely happens in main.lua.
Rob