i have use banner and inneractive admob then error. but only use banner no index error. This problem on going to next scene.
module(…, package.seeall)
widget = require (“widget”)
local widget=widget
– AdMob Function Decleration
local appID = “ca-app-pub-9490586669956065/9942080439”
local appIDIN = “ca-app-pub-9490586669956065/2418813633”
local ads = require(“ads”)
local t
local function adListener(event)
local msg = evento.response
print(msg)
if event.phase == “loaded” then
end
if event.isError then
end
end
ads.init(“admob”, appID, adListener)
ads.init(“admob”, appIDIN, adListener)
local function showBanner()
ads.show(“banner”, {x = 2, y = display.viewableContentHeight-5, appID = “ca-app-pub-9490586669956065/9942080439”,testMode = true})
end
local function randomDelay()
return math.random(12000)
end
local function onTimer()
ads.show( “interstitial”, { x=0, y=0 ,appID = “ca-app-pub-9490586669956065/2418813633”, testMode = true})
end
– Main function - MUST return a display.newGroup()
function new()
local hudGroup = display.newGroup()
local startNewRound = function()
if ghostObject then
local activateRound = function()
– Show the pause button
pauseBtn.isVisible = true
pauseBtn.isActive = true
restartBtnTop.isVisible = true
restartBtnTop.isActive = true
– START up and down animation
if ghostTween then
transition.cancel( ghostTween )
end
end
end
local setScore = function( scoreNum )
local newScore = scoreNum
gameScore = newScore
if gameScore < 0 then gameScore = 0; end
print(“Your Score is ============”)
print(gameScore)
print(comma_value(gameScore))
scoreText.text = comma_value(gameScore)
scoreText.xScale = 0.5; scoreText.yScale = 0.5 --> for clear retina display text
scoreText.x = (480 - (scoreText.contentWidth * 0.5)) - 15
scoreText.y = display.contentHeight/8
end
local callGameOver = function( isWin )
local isWin = isWin
if isWin == “yes” then
audio.play( youWinSound )
else
audio.play( youLoseSound )
end
gameIsActive = false --> temporarily disable gameplay touches, enterFrame listener, etc.
physics.pause()
– Make sure pause button is hidden/inactive
pauseBtn.isVisible = false
pauseBtn.isActive = false
restartBtnTop.isVisible = false
restartBtnTop.isActive = false
if continueTimer then timer.cancel( continueTimer ); end
continueText.isVisible = false
– Create all game over objects and insert them into the HUD group
– SHADE
local shadeRect = display.newRect( 0, 0, 480*3, 320*3 )
shadeRect:setFillColor( 0, 0, 0, 255 )
shadeRect.alpha = 0
– GAME OVER WINDOW
local gameOverDisplay
if isWin == “yes” then
gameOverDisplay = display.newImageRect( “youwin.png”, 390, 154 )
– Give score bonus depending on how many ghosts left
local ghostBonus = gameLives * 20000
local newScore = gameScore + ghostBonus
setScore( newScore )
else
gameOverDisplay = display.newImageRect( “youlose.png”, 390, 154 )
end
gameOverDisplay.x = 240; gameOverDisplay.y = 165
gameOverDisplay.alpha = 0
– MENU BUTTON
local onMenuTouch = function( event )
if event.phase == “release” then
audio.play( tapSound )
director:changeScene( “mainmenu” )
end
end
local menuBtn = ui.newButton{
defaultSrc = “menubtn.png”,
defaultX = 60,
defaultY = 60,
overSrc = “menubtn.png”,
overX = 60,
overY = 60,
onEvent = onMenuTouch,
id = “MenuButton”,
text = “”,
font = “Helvetica”,
textColor = { 255, 255, 255, 255 },
size = 16,
emboss = false
}
if isWin == “yes” then
menuBtn.x = 227
else
menuBtn.x = 266
end
menuBtn.y = 186
menuBtn.alpha = 0
– RESTART BUTTON
local onRestartTouch = function( event )
if event.phase == “release” then
audio.play( tapSound )
local theModule = “load” … restartModule
director:changeScene( theModule )
end
end
local restartBtn = ui.newButton{
defaultSrc = “restartbtn.png”,
defaultX = 60,
defaultY = 60,
overSrc = “restartbtn.png”,
overX = 60,
overY = 60,
onEvent = onRestartTouch,
id = “RestartButton”,
text = “”,
font = “Helvetica”,
textColor = { 255, 255, 255, 255 },
size = 16,
emboss = false
}
restartBtn.x = menuBtn.x + 72; restartBtn.y = 186
restartBtn.alpha = 0
– NEXT BUTTON
local onNextTouch = function( event )
if event.phase == “release” then
audio.play( tapSound )
local theModule = “load” … nextModule
director:changeScene( theModule )
end
end
local nextBtn = ui.newButton{
defaultSrc = “nextlevelbtn.png”,
defaultX = 60,
defaultY = 60,
overSrc = “nextlevelbtn.png”,
overX = 60,
overY = 60,
onEvent = onNextTouch,
id = “NextButton”,
text = “”,
font = “Helvetica”,
textColor = { 255, 255, 255, 255 },
size = 16,
emboss = false
}
nextBtn.x = restartBtn.x + 72; nextBtn.y = 186
nextBtn.alpha = 0
if isWin ~= “yes” then nextBtn.isVisible = false; end
– OPENFEINT BUTTON
local onOFTouch = function( event )
if event.phase == “release” then
audio.play( tapSound )
– Launch OpenFeint Leaderboards Panel:
--openfeint.launchDashboard(“leaderboards”)
end
end
– INSERT ALL ITEMS INTO GROUP
hudGroup:insert( shadeRect )
hudGroup:insert( gameOverDisplay )
hudGroup:insert( menuBtn )
hudGroup:insert( restartBtn )
if isWin == “yes” then updateData(4, “lockOn”, 0); hudGroup:insert( nextBtn ); end
– FADE IN ALL GAME OVER ELEMENTS
transition.to( shadeRect, { time=200, alpha=0.65 } )
transition.to( gameOverDisplay, { time=500, alpha=1 } )
transition.to( menuBtn, { time=500, alpha=1 } )
transition.to( restartBtn, { time=500, alpha=1 } )
if isWin == “yes” then updateData(4, “lockOn”, 0); transition.to( nextBtn, { time=500, alpha=1 } ); end
– MAKE SURE SCORE TEXT IS VISIBLE (if player won the round)
if isWin == “yes” then
scoreText.isVisible = false
local oldScoreText = scoreText.text
scoreText.text = "Score: " … oldScoreText
scoreText.xScale = 0.5; scoreText.yScale = 0.5 --> for clear retina display text
scoreText.x = (480 - (scoreText.contentWidth * 0.5)) - 30
scoreText.y = 30
scoreText:toFront()
timer.performWithDelay( 1000, function() scoreText.isVisible = true; end, 1 )
else
--scoreText:removeSelf()
display.remove( scoreText )
scoreText = nil
end
– Update Best Score
if gameScore > bestScore then
bestScore = gameScore
local bestScoreFilename = restartModule … “.data”
saveValue( bestScoreFilename, tostring(bestScore) )
end
– MAKE SURE BEST SCORE TEXT IS VISIBLE
bestScoreText = display.newText( “0”, 10, 300, “Helvetica-Bold”, 32 )
bestScoreText:setTextColor( 228, 228, 228, 255 ) --> white
bestScoreText.text = "Best Score For This Level: " … comma_value( bestScore )
bestScoreText.xScale = 0.5; bestScoreText.yScale = 0.5 --> for clear retina display text
bestScoreText.x = (bestScoreText.contentWidth * 0.5) + 15
bestScoreText.y = 304
updateData(1, “score”, comma_value( bestScore ));
hudGroup:insert( bestScoreText )
end
local callNewRound = function( shouldPoof, instantPoof )
local shouldPoof = shouldPoof
local instantPoof = instantPoof
local isGameOver = false
if blastGlow.isVisible then
blastGlow.isVisible = false
end
if gameLives >= 1 then
gameLives = gameLives - 1
if gameLives == 4 then
life5.alpha = 0.3
if monsterCount < 1 then isGameOver = true; end
elseif gameLives == 3 then
life5.alpha = 0.3
life4.alpha = 0.3
if monsterCount < 1 then isGameOver = true; end
elseif gameLives == 2 then
life5.alpha = 0.3
life4.alpha = 0.3
life3.alpha = 0.3
if monsterCount < 1 then isGameOver = true; end
elseif gameLives == 1 then
life5.alpha = 0.3
life4.alpha = 0.3
life3.alpha = 0.3
life2.alpha = 0.3
if monsterCount < 1 then isGameOver = true; end
elseif gameLives == 0 then
life5.alpha = 0.3
life4.alpha = 0.3
life3.alpha = 0.3
life2.alpha = 0.3
life1.alpha = 0.3
isGameOver = true
end
elseif gameLives < 0 then
gameLives = 0
life5.alpha = 0.3
life4.alpha = 0.3
life3.alpha = 0.3
life2.alpha = 0.3
life1.alpha = 0.3
isGameOver = true
else
life5.alpha = 0.3
life4.alpha = 0.3
life3.alpha = 0.3
life2.alpha = 0.3
life1.alpha = 0.3
isGameOver = true
end
if shouldPoof then
local poofTheGhost = function()
local theDelay = 500
– Make ghost disappear and show “poof” animation
ghostObject:setLinearVelocity( 1, 1 )
ghostObject.bodyType = “static”
ghostObject.isVisible = false
ghostObject.isBodyActive = false
ghostObject.rotation = 0
– Poof code below –
audio.play( ghostPoofSound )
poofObject.x = ghostObject.x; poofObject.y = ghostObject.y
poofObject.alpha = 0
poofObject.isVisible = true
local fadePoof = function()
transition.to( poofObject, { time=2000, alpha=0 } )
end
transition.to( poofObject, { time=100, alpha=1.0, onComplete=fadePoof } )
– Move camera to far right to see effect
if gameGroup.x > -480 then
--local camTween = transition.to( gameGroup, { time=500, x=-480 } )
end
waitingForNewRound = false
if continueTimer then timer.cancel( continueTimer ); end
continueText.isVisible = false
print( “Hello Corona user” )
print(gameLives)
print (monsterCount)
if gameLives < 1 then
– GAME OVER
if monsterCount < 1 then
callGameOver( “yes” )
else
callGameOver( “no” )
end
elseif monsterCount < 1 and gameLives >= 1 then
callGameOver( “yes” )
else
startNewRound()
end
end
if instantPoof == “yes” then
local poofTimer = timer.performWithDelay( 4000, poofTheGhost, 1 )
else
local poofTimer = timer.performWithDelay( 4000, poofTheGhost, 1 )
end
else
--ghostObject:setLinearVelocity( 0, 0 )
ghostObject.bodyType = “static”
ghostObject.isVisible = false
ghostObject.isBodyActive = false
ghostObject.rotation = 0
--restartTimer = timer.performWithDelay( 300, startNewRound, 1 )
if not isGameOver then
restartTimer = timer.performWithDelay( 300, startNewRound, 1 )
else
if monsterCount > 0 then
restartTimer = timer.performWithDelay( 500, function() callGameOver( “no” ); end, 1 )
else
restartTimer = timer.performWithDelay( 500, function() callGameOver( “yes” ); end, 1 )
end
end
end
end
local drawBackground = function()
– Background gets drawn in this order: backdrop, clouds, trees, red glow
– BACKDROP
backgroundImage1 = display.newImageRect( backgroundFilename1, 480, 320 )
--backgroundImage1:setReferencePoint( display.CenterLeftReferencePoint )
backgroundImage1.anchorX = 0.0
backgroundImage1.anchorX = -display.contentHeight / 2
backgroundImage1.x = 0; backgroundImage1.y = 160
backgroundImage2 = display.newImageRect( backgroundFilename2, 480, 320 )
--backgroundImage2:setReferencePoint( display.CenterLeftReferencePoint )
backgroundImage2.anchorX = 0.0
backgroundImage2.anchorX = -display.contentHeight / 2
backgroundImage2.x = 480; backgroundImage2.y = 160
gameGroup:insert( backgroundImage1 )
gameGroup:insert( backgroundImage2 )
–
--load sprite module
local sprite = require( “sprite” )
– load spritesheet image
– 1st parameter is a path to the image file
– 2nd frame width
– 3rd frame height
local pteroSheet = sprite.newSpriteSheet( “bat.png”,100, 120 )
local pteroSet = sprite.newSpriteSet( pteroSheet, 1, 2 )
sprite.add( pteroSet, “ptero”, 1, 2, 2000, -2 )
--create an instance of our animated sprite
local ptero = sprite.newSprite( pteroSet )
--center it on screen
--ptero:setReferencePoint( display.CenterReferencePoint )
ptero.anchorX = -display.contentWidth/2
ptero.anchorX = -display.contentHeight/2
ptero.x = display.contentWidth-display.contentWidth/8-100
ptero.y = display.contentHeight / 2.5
gameGroup:insert(ptero)
– start animating
ptero:prepare( “ptero” )
ptero:play()
--ADmob
– CLOUDS
clouds1 = display.newImageRect( “trees-left.png”, 480, 320 )
clouds1.x = 240; clouds1.y = 160
clouds2 = display.newImageRect( “trees-left.png”, 480, 320 )
clouds2.x = 720; clouds2.y = 160
clouds3 = display.newImageRect( “trees-left.png”, 480, 320 )
clouds3.x = 1200; clouds3.y = 160
clouds4 = display.newImageRect( “trees-left.png”, 480, 320 )
clouds4.x = 1680; clouds4.y = 160
gameGroup:insert( clouds1 )
gameGroup:insert( clouds2 )
gameGroup:insert( clouds3 )
gameGroup:insert( clouds4 )
– TREES
local treesLeft = display.newImageRect( “trees-left.png”, 480, 320 )
treesLeft.x = 240; treesLeft.y = 160
local treesRight = display.newImageRect( “trees-left.png”, 480, 320 )
treesRight.x = 720; treesRight.y = 160
gameGroup:insert( treesLeft )
gameGroup:insert( treesRight )
– RED GLOW
--[[
local redGlow = display.newImageRect( “redglow.png”, 480, 320 )
redGlow.x = 725; redGlow.y = 160
redGlow.alpha = 0.5
gameGroup:insert( redGlow )
]]–
end
local drawHUD = function()
– TWO BLACK RECTANGLES AT TOP AND BOTTOM (for those viewing from iPad)
local topRect = display.newRect( 0, -160, 480, 160 )
topRect:setFillColor( 1, 0, 0 )
topRect.alpha = 0.0
local bottomRect = display.newRect( 0, 320, 480, 160 )
bottomRect:setFillColor( 1, 0, 0 )
bottomRect.alpha = 0.0
hudGroup:insert( topRect )
hudGroup: insert( bottomRect )
– LIVES DISPLAY
life1 = display.newImageRect( “lifeicon.png”, 15, 15 )
life1.x = 40; life1.y = display.contentHeight/8
life2 = display.newImageRect( “lifeicon.png”, 15, 15 )
life2.x = life1.x + 20; life2.y = display.contentHeight/8
life3 = display.newImageRect( “lifeicon.png”, 15, 15 )
life3.x = life2.x + 20; life3.y = display.contentHeight/8
life4 = display.newImageRect( “lifeicon.png”, 15, 15 )
life4.x = life3.x + 20; life4.y = display.contentHeight/8
life5 = display.newImageRect( “lifeicon.png”, 15, 15 )
life5.x = life4.x + 20; life5.y = display.contentHeight/8
hudGroup:insert( life1 )
hudGroup:insert( life2 )
hudGroup:insert( life3 )
hudGroup:insert( life4 )
hudGroup:insert( life5 )
– SCORE DISPLAY
scoreText = display.newText( “0”, 470, 22, “Helvetica-Bold”, 52 )
scoreText:setTextColor( 255, 255, 255, 255 ) --> white
scoreText.text = gameScore
scoreText.xScale = 0.5; scoreText.yScale = 0.5 --> for clear retina display text
scoreText.x = (480 - (scoreText.contentWidth * 0.5)) - 15
scoreText.y = display.contentHeight/8
scoreText.alpha = 0.0001
hudGroup:insert( scoreText )
– TAP TO CONTINUE DISPLAY
continueText = display.newText( “TAP TO CONTINUE”, 240, 18, “Helvetica”, 36 )
continueText:setTextColor( 249, 203, 64, 255 )
continueText.xScale = 0.5; continueText.yScale = 0.5
continueText.x = 240; continueText.y = display.contentHeight/8
continueText.isVisible = false
hudGroup:insert( continueText )
– PAUSE BUTTON
local onPauseTouch = function( event )
if event.phase == “release” and pauseBtn.isActive then
audio.play( tapSound )
– Pause the game
if gameIsActive then
gameIsActive = false
physics.pause()
– SHADE
if not shadeRect then
shadeRect = display.newRect( 0, 0, 480*3, 320*3 )
shadeRect:setFillColor( 0, 0, 0, 255 )
hudGroup:insert( shadeRect )
end
shadeRect.alpha = 0.5
– SHOW MENU BUTTON
if pauseMenuBtn then
pauseMenuBtn.isVisible = true
pauseMenuBtn.isActive = true
pauseMenuBtn:toFront()
end
pauseBtn:toFront()
restartBtnTop:toFront()
– STOP GHOST ANIMATION
if ghostTween then
transition.cancel( ghostTween )
end
else
if shadeRect then
--shadeRect:removeSelf()
display.remove( shadeRect )
shadeRect = nil
end
if pauseMenuBtn then
pauseMenuBtn.isVisible = false
pauseMenuBtn.isActive = false
end
gameIsActive = true
physics.start()
– START Ghost animation back up
if ghostTween then
transition.cancel( ghostTween )
end
local function ghostAnimation()
local animUp = function()
if ghostObject.inAir or shotOrb.isVisible then
transition.cancel( ghostTween )
else
ghostTween = transition.to( ghostObject, { time=375, y=195, onComplete=ghostAnimation })
end
end
if ghostObject.inAir or shotOrb.isVisible then
transition.cancel( ghostTween )
else
ghostTween = transition.to( ghostObject, { time=375, y=195, onComplete=animUp })
end
end
ghostTween = transition.to( ghostObject, { time=375, y=195, onComplete=ghostAnimation })
end
end
end
pauseBtn = ui.newButton{
defaultSrc = “pausebtn.png”,
defaultX = 44,
defaultY = 44,
overSrc = “pausebtn.png”,
overX = 44,
overY = 44,
onEvent = onPauseTouch,
id = “PauseButton”,
text = “”,
font = “Helvetica”,
textColor = { 255, 255, 255, 255 },
size = 16,
emboss = false
}
pauseBtn.x = 442; pauseBtn.y = display.viewableContentHeight-25
pauseBtn.isVisible = false
pauseBtn.isActive = false
--Reload Button
local onRestartTouchTop = function( event )
if event.phase == “release” then
audio.play( tapSound )
local theModule = “load” … restartModule
director:changeScene( theModule )
end
end
restartBtnTop = ui.newButton{
defaultSrc = “restartbtn.png”,
defaultX = 40,
defaultY = 40,
overSrc = “restartbtn.png”,
overX = 40,
overY = 40,
onEvent = onRestartTouchTop,
id = “RestartButtonTop”,
text = “”,
font = “Helvetica”,
textColor = { 255, 255, 255, 255 },
size = 16,
emboss = false
}
restartBtnTop.x = display.contentWidth-40; restartBtnTop.y = 40;
restartBtnTop.isVisible = false
restartBtnTop.isActive = false
hudGroup:insert( restartBtnTop )
hudGroup:insert( pauseBtn )
– MENU BUTTON (on Pause Display)
local onMenuPauseTouch = function( event )
if event.phase == “release” and pauseMenuBtn.isActive then
audio.play( tapSound )
local onComplete = function ( event )
if “clicked” == event.action then
local i = event.index
if i == 2 then
– Player click ‘Cancel’; do nothing, just exit the dialog
elseif i == 1 then
– Player clicked Yes, go to main menu
director:changeScene( “mainmenu” )
end
end
end
– Show alert with two buttons
local alert = native.showAlert( “Are You Sure?”, “Your current game will end.”,
{ “Yes”, “Cancel” }, onComplete )
end
end
pauseMenuBtn = ui.newButton{
defaultSrc = “pausemenubtn.png”,
defaultX = 44,
defaultY = 44,
overSrc = “pausemenubtn.png”,
overX = 44,
overY = 44,
onEvent = onMenuPauseTouch,
id = “PauseMenuButton”,
text = “”,
font = “Helvetica”,
textColor = { 255, 255, 255, 255 },
size = 16,
emboss = false
}
pauseMenuBtn.x = 38; pauseMenuBtn.y = display.viewableContentHeight-25
pauseMenuBtn.isVisible = false
pauseMenuBtn.isActive = false
hudGroup:insert( pauseMenuBtn )
end
local createLevel = function()
– AdMob Time Delay
timer.performWithDelay( randomDelay(), onTimer, 1)
t = timer.performWithDelay(50, showBanner, 1)
reorderLayers()
end
gameInit()
– MUST return a display.newGroup()
return gameGroup
end