Help using admob

I’ve been trying to add admob ads to my app, but i get the same error all the time, so my question is if someone could give the code and tell what lua files each code need to be in for the ads to work. would appriciate this big time! 

Hi,

Try this:

I place this code in main.lua

[lua]

local provider = “admob”

– replace with your own application ID
local appId = “ca-app-pub-663…”

– load Corona ‘ads’ library
ads = require “ads”

– show an ad if one has been downloaded and is available for playback
function showAd()
    local adX, adY = display.screenOriginX,display.screenOriginY
    ads.show( “banner”, { x = adX, y = adY} )  – for admob
end

– create a ‘function’ ad listener
function functionAdListener( event )
    if event.type == “adStart” and event.isError then
        – wait 5 seconds before retrying to display ad
        timer.performWithDelay(5000, showAd)
        – ad displayed and then closed
    elseif event.type == “adEnd” then

    else
        print( “Received event”)
    end
end

– init the ads
ads.init( provider, appId, functionAdListener )

[/lua]

Then in the scene where you want to show an add ( in my case - menu.lua ) I just type :

[lua] showAd() [/lua]

Also in build.settings you need to place this:

[lua]

plugins =
        {
            – key is the name passed to Lua’s ‘require()’
            [“CoronaProvider.ads.admob”] =
            {
                – required
                publisherId = “com.coronalabs”,
            },

}

[/lua]

Hope this helps :slight_smile:

Hi everyone,

thank you very much element_skate for your advice. Just a question.

I placed your code in my main.lua and then I wrote showAd() in the CreateScene in the menu.lua scene. The banner is ok but the problem is that when I change scenes the banner remains.

All I want is to show ads in the menu scene but not in the level scenes of my app game.

I tried writing ads.hide() in the DestroyScene but it doesn’t work… The banner keeps showing in all the app scenes.

Problem solved.

It’s really simple: I placed ads.show() in the CreateScene and ads.hide() in the ExitScene and now it works fine!  :wink:

Hi,

Try this:

I place this code in main.lua

[lua]

local provider = “admob”

– replace with your own application ID
local appId = “ca-app-pub-663…”

– load Corona ‘ads’ library
ads = require “ads”

– show an ad if one has been downloaded and is available for playback
function showAd()
    local adX, adY = display.screenOriginX,display.screenOriginY
    ads.show( “banner”, { x = adX, y = adY} )  – for admob
end

– create a ‘function’ ad listener
function functionAdListener( event )
    if event.type == “adStart” and event.isError then
        – wait 5 seconds before retrying to display ad
        timer.performWithDelay(5000, showAd)
        – ad displayed and then closed
    elseif event.type == “adEnd” then

    else
        print( “Received event”)
    end
end

– init the ads
ads.init( provider, appId, functionAdListener )

[/lua]

Then in the scene where you want to show an add ( in my case - menu.lua ) I just type :

[lua] showAd() [/lua]

Also in build.settings you need to place this:

[lua]

plugins =
        {
            – key is the name passed to Lua’s ‘require()’
            [“CoronaProvider.ads.admob”] =
            {
                – required
                publisherId = “com.coronalabs”,
            },

}

[/lua]

Hope this helps :slight_smile:

Hi everyone,

thank you very much element_skate for your advice. Just a question.

I placed your code in my main.lua and then I wrote showAd() in the CreateScene in the menu.lua scene. The banner is ok but the problem is that when I change scenes the banner remains.

All I want is to show ads in the menu scene but not in the level scenes of my app game.

I tried writing ads.hide() in the DestroyScene but it doesn’t work… The banner keeps showing in all the app scenes.

Problem solved.

It’s really simple: I placed ads.show() in the CreateScene and ads.hide() in the ExitScene and now it works fine!  :wink:

Please help me guys. I dont have ads.lua file. I dont know from where to download that.

There is no “ads.lua”.  It’s an internal module.  You just have to add the line:

local ads = require( “ads” )

near the top of your code before you call any of the ads.* API calls.

Rob

Yes i know that Rob. But i get an error on that statement : local ads=require(“ads”)

I am using corona starter edition on my windows PC. Is this there any problem in corona’s windows version

My error:

Runtime error

        F:\Android app development\The Red Airship\main.lua:22: module ‘ads’ not

 found:resource (ads.lu) does not exist in archive

        no field package.preload[‘ads’]

        no file ‘F:\Android app development\The Red Airship\ads.lua’

        no file 'G:\Program FRuntime error: F:\Android app development\The Red A

irship\main.lua:22: module ‘ads’ not found:resource (ads.lu) does not exist in archive

Please help me guys. I dont have ads.lua file. I dont know from where to download that.

There is no “ads.lua”.  It’s an internal module.  You just have to add the line:

local ads = require( “ads” )

near the top of your code before you call any of the ads.* API calls.

Rob

Yes i know that Rob. But i get an error on that statement : local ads=require(“ads”)

I am using corona starter edition on my windows PC. Is this there any problem in corona’s windows version

My error:

Runtime error

        F:\Android app development\The Red Airship\main.lua:22: module ‘ads’ not

 found:resource (ads.lu) does not exist in archive

        no field package.preload[‘ads’]

        no file ‘F:\Android app development\The Red Airship\ads.lua’

        no file 'G:\Program FRuntime error: F:\Android app development\The Red A

irship\main.lua:22: module ‘ads’ not found:resource (ads.lu) does not exist in archive