I have a simple app that I am making where asteroids fall from the sky, and when they collide with the character (flying frog…) the scene changes to a try again screen. I have everything working, however when the asteroid collides with the character and the code tries to change to tryAgain() scene function, I get an error “attempt to call global tryAgain (a nil value)”.
tryAgain() is my try again scene.
Not sure what I’m doing wrong… here is the code, the tryAgain() scene function is at the end of the code, and the error is happening on line 14 when the collision happens. I have bolded the problem code.
Any help would be greatly appreciated!
Thanks in advance!
display.setStatusBar(display.HiddenStatusBar)
local physics = require("physics")
physics.start()
physics.setDrawMode("hybrid")
physics.setGravity(0,0)
local W = display.contentWidth
local H = display.contentHeight
local asteroid
local frog
local background
local button
local text
local mRandom=math.random
**local function astCollision(self, event)
if event.phase == "began" then
if event.target.type == "asteroid" and event.other.type == "frog" then
tryAgain()
end
end
end**
local function asteroids()
asteroid = display.newImage("Images/ast.png")
asteroid.width = 256
asteroid.height = 256
asteroid.x = mRandom(128,W-128)
asteroid.y = - 256
physics.addBody(asteroid, {0,0,0})
asteroid:setLinearVelocity(0,600)
asteroid.collision = astCollision
asteroid:addEventListener("collision", asteroid)
asteroid.type = "asteroid"
end
local function frog()
frog = display.newImage("Images/Frog6.png")
frog.x = W/2
frog.y = H/1.2
frog.width = 96
frog.height = 128
physics.addBody(frog, "static", {0,0,0})
frog.type = "frog"
end
local function background()
background = display.newImage("Images/Background.png")
background.x = W/2
background.y = H/2
background.width = W
background.height = H
end
local function button()
button = display.newImage("Images/bluebutton.png")
button.x = W/2
button.y = H/2
end
local function mainMenu()
background()
button()
text = display.newText("Play Game",W/2,H/2,nativesystemFont,64)
end
local function mainGame()
background()
frog()
timer.performWithDelay(1000,asteroids,0)
end
local function tryAgain()
background()
button()
text = display.newText("Try Again",W/2,H/2,nativesystemFont,64)
end
mainGame()