iAds in Composer Help

If skimming read  BOLD 

I really need help. I’m about to launch my companies first app and have a few bugs to work out. The main thing is ads right now. 

In my gameOver scene when the user clicks the restart button the game goes back to the menu.lua everything works great but when I go back into my scene the ad goes off screen then back on the screen. I’m assuming it’s because menu.lua is calling ads.show again. 

My Question?

What do I need to do so that when an ad is not displaying, the menu.lua scene will ask apple for another ad? At the same time if there is an add already being displayed I don’t need to call for another ad if one is already being displayed. I don’t want it too flicker but I want to monetize to the fullest extent. 

display.setStatusBar( display.HiddenStatusBar )

local composer = require(“composer”)

local scene = composer.newScene()

– The name of the ad provider.

local adNetwork = “iads”  – iAds

– Replace with your own application ID

local appID = “com.novateur.redlight”-- iAds

– Load Corona ‘ads’ library

local ads = require “ads”-- iAds

local score = require( “score” )

local mydata = require( “mydata” )

local mydata2 = require( “mydata2” )

local mydata3 = require( “mydata3” )

local buttonClick = audio.loadSound( “buttonClick.m4a”)

– Set up ad listener.

local function adListener( event )-- iAds

– event table includes:

– event.provider

– event.isError (e.g. true/false )

– event.response - the localized description of the event (error or confirmation message)

local msg = event.response

– just a quick debug message to check what response we got from the library

print(" Message received from the ads library: ", msg)

if event.isError then

print( “Error” … msg)

else

print( “Success” … msg)

end

end

ads.init( adNetwork, appID, adListener )-- iAds

function startGame(event)

if event.phase == “ended” then 

audio.play( buttonClick )

composer.gotoScene(“game”, “slideLeft”, 1000)

– setVolume( 0.0)

end 

end 

settings = require(“settings”)

settings.load()

 function appRun()

    if (settings.get(‘timesRun2’)) == nil then

        timesRun2 = 1

        --print (“Saving Times Run “…timesRun…” to settings.json”)

        settings.set(‘timesRun2’, timesRun2)

        --print (" Times Run is:"…(settings.get(‘timesRun’))…" from settings.json")

    else

        timesRun2 = settings.get(‘timesRun2’)

        timesRun2 = timesRun2 + 1

        --print (“Saving Times Run “…timesRun…” to settings.json”)

        settings.set(‘timesRun2’, timesRun2)

        --print (" Times Run is:"…(settings.get(‘timesRun’))…" from settings.json")

        if timesRun2 == 3 then

            print “Showing ‘Rate Me’ popup”

            local function onComplete( event )

                if “clicked” == event.action then

                    local i = event.index

                    if 1 == i then

                            print “OK Clicked”

                            system.openURL( “itms-apps://itunes.apple.com/app/pages/id555435191?mt=8&uo=4” )

                    elseif 2 == i then

                            print “Cancel Clicked”

                    end

                end

            end

            local alert = native.showAlert( “Check out my other apps!”, “Please rate this app to help support future development!”, { “OK”, “No Thanks” }, onComplete )

        end

    end

end

appRun()

function muting(event)

print( “Step 1” )

taps = taps + 1  

taps2 = taps/2 

taps3 = math.floor( taps2 )

print("Taps  ==  " … taps)

print( "Taps 2 == " … taps2 )

print( "Taps3 == " … taps3 )

print( “Step 1” )

print( soundOn )

if taps2 == taps3  then – Turn sound on

audio.play( buttonClick )

soundOn = true

star.isVisible = false

audio.setVolume( 1.0)

print( “Sound ON” )

      mydata3.muteCheck = 0   – Makes so that when volume is on and the user enters menu scene that the redSlash isn’t there

end

if taps2 ~= taps3 then – Turns sound off 

audio.play( buttonClick )

star.isVisible = true 

print( soundOn )

print( “Sound OFF” )

audio.setVolume( 0.0)

soundOn = false

print( soundOn )

   mydata3.muteCheck = 1

end

end

function twitter(event)

audio.play( buttonClick )

system.openURL( “http://www.twitter.com” )

end 

function facebook( event )

audio.play( buttonClick )

system.openURL( “http://www.facebook.com” )

end

function customerService(event)

audio.play( buttonClick )

local options =

{

   to = “vernc227@gmail.com”,

  – subject = “My High Score”,

  – body = “I scored over 9000!!! Can you do better?”,

}

native.showPopup( “mail”, options )

end

function rate( event )

audio.play( buttonClick )

local options =

{

   iOSAppId = “555435191”,

}

native.showPopup( “appStore”, options )

–system.openURL( “itms-apps://itunes.apple.com/app/pages/id555435191?mt=8&uo=4” )

end

function scene:create( event )

local sceneGroup = self.view 

mainMenuScreen = display.newImageRect(“menuScreen.png”,display.actualContentWidth, display.actualContentHeight)

mainMenuScreen.x = display.contentCenterX

mainMenuScreen.y = display.contentCenterY 

sceneGroup:insert(mainMenuScreen)

print("Image Suffix " …  display.pixelWidth / display.actualContentWidth )

playBtn = display.newCircle( display.contentCenterX , display.contentCenterY - 110, 210 )

playBtn:setFillColor( 0.5 )

playBtn.strokeWidth = 5

playBtn.alpha = .01

sceneGroup:insert(playBtn)

muteBtn = display.newCircle( 540, 770, 50 )

muteBtn:setFillColor( 0.5 )

muteBtn.alpha = .01

sceneGroup:insert(muteBtn)

twitterBtn = display.newCircle( 495, 945, 50 )

twitterBtn:setFillColor( 0.5 )

twitterBtn.alpha = .01

sceneGroup:insert(twitterBtn)

facebookBtn = display.newCircle( 128, 945, 50 )

facebookBtn:setFillColor( 0.5 )

facebookBtn.alpha = .01

sceneGroup:insert(facebookBtn)

customerServiceBtn = display.newCircle( display.contentCenterX - 8, 972, 50 )

customerServiceBtn:setFillColor( 0.5 )

customerServiceBtn.alpha = .01

sceneGroup:insert(customerServiceBtn)

rateBtn = display.newCircle( 95, 760, 50 )

rateBtn:setFillColor( 0.5 )

rateBtn.alpha = .01

sceneGroup:insert(rateBtn)

   star = display.newLine( 477, 840, 500, 840 )

star:setStrokeColor( 1, 0, 0, 1 )

star.isVisible = false

sceneGroup:insert(star)

taps = 0

bestTitle = display.newText( “Best”, display.contentCenterX, display.contentCenterY + 190 , “Calibri Light”, 90 )

bestTitle:setTextColor( 255, 0, 0)

sceneGroup:insert(bestTitle)

bestScore = settings.get(‘timesRun’)

bestScore2 = tostring( bestScore )

if bestScore2 == nil then 

bestScore2 = tostring( 0 )

end 

best = display.newText( bestScore2, display.contentCenterX, display.contentCenterY + 260 , “Calibri”, 80 )

best:setTextColor( 0, 0, 0)

sceneGroup:insert(best)

if mydata3.muteCheck == 1 then 

taps = 1 

star.isVisible = true

print( “YOU ALREADY KNOW THOUGH” )

end 

end

function scene:show( event)

local sceneGroup = self.view 

local phase = event.phase 

if(phase == “will”)then 

   playBtn:addEventListener( “touch”, startGame )

muteBtn:addEventListener(“tap”,muting)

twitterBtn:addEventListener( “tap”, twitter )

facebookBtn:addEventListener( “tap”, facebook )

customerServiceBtn:addEventListener( “tap”, customerService )

rateBtn:addEventListener( “tap”, rate )

mainMenuScreen.x = display.contentCenterX

mainMenuScreen.y = display.contentCenterY 

local a = composer.getVariable(“Best”) – Dont need 

print( a )

elseif( phase ==“did”)then 

print( “spawned” )

end 

–if event.is

 ads.show( “banner”, { x=0, y=960 * 1.6  })    – iAds  

   

end 

function scene:hide( event )

local sceneGroup = self.view

local phase = event.phase 

if( phase == “will”) then 

   playBtn:removeEventListener( “touch”, startGame )

muteBtn:removeEventListener(“tap”,muting)

twitterBtn:removeEventListener( “tap”, twitter )

facebookBtn:removeEventListener( “tap”, facebook )

customerServiceBtn:removeEventListener( “tap”, customerService )

rateBtn:removeEventListener( “tap”, rate )

print("scene is dead1 ")

elseif( phase == “did”) then 

print("scene is dead2 ")

end 

end

function scene:destroy( event )

local sceneGroup = self.view

print(“destroy build destroy”)

end

– Listener setup

scene:addEventListener( “create”, scene)

scene:addEventListener( “show”, scene )

scene:addEventListener( “hide”, scene )

scene:addEventListener( “destroy”, scene )

return scene