local inMobi = require( "plugin.inMobi" ) -- Pre-declare a placement ID local placementID = "HereAreNumbers" -- This was changed for this post local function adListener( event ) if ( event.phase == "init" ) then -- Successful initialization -- Load a banner ad inMobi.load( "interstitial", placementID ) elseif ( event.phase == "failed" ) then -- The ad failed to load print( event.type ) print( event.placementId ) print( event.isError ) print( event.response ) end end -- Initialize the InMobi plugin inMobi.init( adListener, { accountId="numbersAndLetters" } ) -- changed for this post -- Sometime later, check if the ad is loaded if ( inMobi.isLoaded( placementID ) ) then -- Show the ad inMobi.show( placementID, { yAlign="bottom" } ) end
And… nothing really works.
In the simulator, it says that I have to test it on an actual device, but it doesn’t work on my phone.
adListener is an asynchronous function. That means that it doesn’t necessarily execute by the time the program moves on to the next line of code.
In your case, you are trying to see if an ad can be shown only a few lines after you’ve initialised the plugin. This happens in an instant and the plugin has not had the time to initialise itself. In your adListener’s “init” phase, you load the first ad, however, this occurs much later than your “inMobi.isLoaded( placementID )” check.
So, simply put, you just need to do is to wait for the plugin to be initialised first. One easy way of testing this on a device would be to create a white rectangle on the screen. Once the “init” phase is done, you can set the rectangle’s fill color to something else. Then, add a touch listener to the rect and check if an ad is available by pressing that.
While its code is for AdMob, the discussion is general to all ad networks and for the most part the code is general enough, concept wise for any ad plugin.
This should be required reading for anyone who wants to add ads to their apps. There is a companion blog post that covers monetization in general. You should read and understand this too:
Thanks for the help, didn’t know that about the adListener.
I did what you said, but still doesn’t work. Its as if the ad never gets initialized. inMobi.isLoaded(placementID) is always false, even when I click on something to check if its true.
When you have problems such as this, the first step should always be to consult the documentation (which Rob has linked). If that doesn’t work, then you should post code of what you’ve done, like you originally did. Now I can only guess and say that “you did it wrong”. Have you setup the plugin properly in your build settings? How do you check if the ad is loaded and when, does your device has Internet connection, etc.?
Showing banner ads during game play isn’t a good idea. Players are focused on game play and you’re just wasting your ad provider’s inventory. You’re attempting to show an add every time you move your player.
You should, on success of the ad loading, get an event that trigger’s your adListener function and maybe there you might want to consider showing the ad and let it auto refresh.
Look at this adListener…
local json = require("json") local function adListener( event ) print( json.prettify( event ) ) if ( event.phase == "init" ) then -- Successful initialization -- Load a banner ad inMobi.load( "banner", placementID ) elseif ( event.phase == "loaded" ) then inMobie.show( placementID ) elseif ( event.phase == "failed" ) then -- The ad failed to load print( event.type ) print( event.placementId ) print( event.isError ) print( event.response ) end end
the big thing here is that you need to print the event table when you enter the adListener function so that you can see the flow of what’s going on. I normally don’t like to show ads after the loading event. But you don’t seem to be using scene management which is a more logical place to manage showing ads. You can show banners on your menu screens, or game over screens etc.
But still we need to know why you’re not getting the ads and the only way to do that is to add the prints that I did and use “adb logcat” to look at an Android device’s console log. If you’re building for iOS, let me know we will need a different way to print the table since apple now combines that print into a useless single line.
Yeah, im not gonna have the banner every time someone touches the screen, I was just testing. Im gonna use the interstitial ad when the player dies or finishes a level (50% chance for each)
Anyhow I checked the log and it didnt recognize the placementID, probably because I didnt enter the app URL on inmobis dashboard ( its mandatory) .