Hello,
I’m currently just playing around with Corona SDK, seeing what I can make and what-not, and came across this problem. On line 54 I get the following error: “attempt to index upvalue ‘turtle’ (a nil value).” This is probably a scope problem but I thought I successfully worked around it by placing a “local turtle” at the very top of my main.lua page?
How can I avoid this in the future?
Thanks,
Angus
-- Remove status bar display.setStatusBar(display.HiddenStatusBar) -- Setting default anchors display.setDefault("anchorX", 0) display.setDefault("anchorY", 0) local physics = require("physics") physics.start() local gameIsActive = true local floor local turtle local gameLayer = display.newGroup() ------------------- -- -- Game Loop ------------------- -- local function gameLoop(event) -- Set up background local background = display.newImage("tpbg.png") background.name = "background" gameLayer:insert(background) -- Set up floor floor = display.newImage("tpfloor.png") floor.y = display.contentHeight - floor.contentHeight floor.name = "floor" gameLayer:insert(floor) function spawnTurtle() -- print("time two") turtle = display.newImage("turtle.png") x = math.random(0, display.contentWidth - turtle.contentWidth) turtle.x = x turtle.y = 0 turtle.name = "turtle" physics.addBody(turtle, "dynamic") end local function checkForCollision(event) if(event.phase == "began") then print(self.myName .. ": collision began with " .. event.other.myName) elseif(event.phase == "ended") then print(self.myName .. ": collision ended with " .. event.other.myName) end end if(gameIsActive == true) then timer.performWithDelay(1000, spawnTurtle, 10) turtle.collision = checkForCollision turtle:addEventListener("collision", turtle) floor.collision = checkForCollision floor:addEventListener("collision", floor) -- print("it worked") end end gameLoop()