InMobi convenience module

Thought I’d share this one.
Makes for easy setup and you also see a placeholder for the ad in the emulator which facilitates testing.

[lua]module(…, package.seeall)

require “ads”

local isDevice = system.getInfo(“environment”) == “device”

local adNetwork = “inmobi”
local appID = “4028cba631d63df101320c65b7e803d3”
local adsTable = {
“banner320x48”,
“banner300x250”,
“banner728x90”,
“banner468x60”,
“banner120x600”
}
local resTable = {
{x=320, y=48},
{x=300, y=250},
{x=728, y=90},
{x=468, y=60},
{x=120, y=600},
}
local dummyAd
local dummyTimer
local adVisible = false

function init()
if isDevice then
ads.init( adNetwork, appID )
end
end

local function stayOnTop()
dummyAd:toFront()
end

function showAd(adIndex, x, y)
if adVisible then return end

if isDevice then
ads.show(adsTable[adIndex], {x=x, y=y, interval=10, testMode=true})
else
dummyAd = display.newRect(x, y, resTable[adIndex].x, resTable[adIndex].y)
dummyTimer = timer.performWithDelay(1000, stayOnTop, 0)
end
adVisible = true
end

function hideAd()
if isDevice then
ads.hide()
else
if dummyTimer then
timer.cancel(dummyTimer)
dummyTimer = nil
end
if dummyAd then
dummyAd:removeSelf()
dummyAd = nil
end
end
adVisible = false
end[/lua]

How to use? Assuming you named the module “inmobi.lua” then

[lua]require “inmobi”

inmobi.init()

– the first param is ad index, in this case 320x48
– the other two params are ad’s position
inmobi.showAd(1, 0, 0)

inmobi.hideAd()[/lua]

Don’t forget to switch the testMode off in code before publishing your app. Also don’t forget to change appId to yours. I won’t be mad if you will, though :slight_smile: [import]uid: 52103 topic_id: 14535 reply_id: 314535[/import]

Nice! [import]uid: 67217 topic_id: 14535 reply_id: 53781[/import]

Updated the code. Now there is a check if the ad is already showing and it now works with Director class. So the dummy ad in simulator won’t be hidden under scene objects when you change scenes, as the timer function will now keep the “ad” on top. [import]uid: 52103 topic_id: 14535 reply_id: 53792[/import]

That sounds great! Thanks, vitalyx, for the update! [import]uid: 67217 topic_id: 14535 reply_id: 53799[/import]

No problem :slight_smile: Another minor change:

from
[lua]function init()
ads.init( adNetwork, appID )
end[/lua]
to
[lua]function init()
if isDevice then
ads.init( adNetwork, appID )
end
end[/lua] [import]uid: 52103 topic_id: 14535 reply_id: 53886[/import]

thank u but i’m facing problem below:
module ‘ads’ not found:resource (ads.lu) does not exist in archive
so can anyone give me solution [import]uid: 82456 topic_id: 14535 reply_id: 54708[/import]

What build of Corona are you using? [import]uid: 52103 topic_id: 14535 reply_id: 54710[/import]

for ios device [import]uid: 82456 topic_id: 14535 reply_id: 54711[/import]

I mean the build number, like 2011.602, for example. You can see it in the Help -> About. [import]uid: 52103 topic_id: 14535 reply_id: 54712[/import]

2011.484 (2011.4.12) [import]uid: 82456 topic_id: 14535 reply_id: 54713[/import]

You need a more recent build like 2011.591 and above. [import]uid: 52103 topic_id: 14535 reply_id: 54716[/import]

This is an awesome forum topic. I will test this once I get home :slight_smile: thanks!

Regards,
Jordan Schuetz
Ninja Pig Studios [import]uid: 29181 topic_id: 14535 reply_id: 55535[/import]

It is a good module for ads. Just a little addition to make it better. The line dummyAd:toBack() needs to be added in the showAd function:

function showAd(adIndex, x, y)
if adVisible then return end

if isDevice then
ads.show(adsTable[adIndex], {x=x, y=y, interval=adsInterval, testMode=adsTestMode})
else
dummyAd = display.newRect(x, y, resTable[adIndex].w, resTable[adIndex].h)
dummyAd:toBack()
dummyTimer = timer.performWithDelay(1000, stayOnTop, 0)
end
adVisible = true
end

Thanks for sharing the code. [import]uid: 42593 topic_id: 14535 reply_id: 56058[/import]

@jinwchen, glad you like it

but why would you want to send ad rect to back (effectively hiding it) inside the showAd function? [import]uid: 52103 topic_id: 14535 reply_id: 56059[/import]

I have tried it out on my app, but the ad rect was shown without any delay (timer). So I sent the ad rect to back when it was created, then the timer takes charge to bring it to front in one second. [import]uid: 42593 topic_id: 14535 reply_id: 56063[/import]

I see. But the delay is not supposed to be there. The timer is used to make sure the ad stays on top even when scene changes (if you use Director class, for example), so it sends the ad to front every second. [import]uid: 52103 topic_id: 14535 reply_id: 56073[/import]

No, I actually decided not to go with ads at all. AdMob hack turns to be forbidden and has ad centering issues. While InMobi slows game down during ad animation, has this hide bug on iOS and doesn’t display ads in certain countries (e.g. Russia). You also can’t cache/preload an ad. That delay, even if it’s one second means a lot. I need an ad displayed instantly.
Hopefully, things will improve soon. But for now I see no choice but selling my game. [import]uid: 52103 topic_id: 14535 reply_id: 56096[/import]

Sorry for misunderstanding your code. I thought you want to mimic the effect of real inmobi ads which will take about one second to load. My app will show or hide the ads depending on which screen the user is seeing.

By the way, do you have any work-around for the crash caused by calling ads.hide() with incomplete loading ? So far, I only can keep calling ads.show() to show or hide(with x,y in negative number to make it off the screen) the ads. It does not take ads = nil for making ads disappear.

* updated - Even calling ads.show() to show or hide still caused crash on my iPad test. [import]uid: 42593 topic_id: 14535 reply_id: 56095[/import]

As far as I know, the inmobi ads did display in Russian Federation in my android game. The inmobi ads in my android game is functioning well and picking up speed along with increasing game downloaded number every day. That is why I am anxious to put the ads in my iOS game. The earned dollars are very limited from paid games unless you got a huge hit game. The money from paid game is one-time deal for developers. But the money from free/lite game keeps rolling as long as the users play the game. [import]uid: 42593 topic_id: 14535 reply_id: 56104[/import]

Thanks for your comments, jinwchen.
Hmm… are you saying that ads are doing better for you than paid downloads? The feedback from people making a couple of cents (on average, I presume) from each download of their free game with InMobi ads is not very encouraging. Even if they keep playing it will be a while till they reach $.70c (you get outright with a one-time deal) in $.02c iterations. Do you have another experience? [import]uid: 52103 topic_id: 14535 reply_id: 56122[/import]