I am using the director class to switch between scenes. main calls menu.lua, where if you tap on a button it takes you to level1.lua, which uses car.lua for the cars in the game.
cars.lua:
[lua]------------------------------------------------------------
–car.lua
–MAKE EVERYTHING IN THIS MODULE VISIBLE
module(…,package.seeall) – yes, I know its bad, I’ll deal with it when everything else is solved
–INCLUDE FILES
local physics = require(“physics”)
local setup = require(“setup”)
local ped = require(“ped”)
require(“ice”)
–LOCALS
…
…
–CAR CREATOR
function new(params)
local newCar
newCar = display.newImage(“car.png”)
–GLOBALS
…
–LOCALS
…
function destroyCars(event)
if (newCar) and endGame == false then
if newCar.y > 320 or newCar.x > 480 then
Runtime:removeEventListener(“enterFrame”, destroyCars)
highScores:increment(“scoresCurrent”, 1)
physics.removeBody(newCar)
newCar:removeSelf()
newCar = nil
end
end
if (newCar) and _G.endGame == true then
Runtime:removeEventListener(“enterFrame”, destroyCars)
physics.removeBody(newCar)
newCar:removeSelf()
newCar = nil
end
end
function newCar.moveCar(event)
…
end
local function delayCar(event) --call this function with a timer.performWithDelay to make the cars wait a certain time after a collision to move.
…
end
function onLocalCollision(event)
…
end
return newCar
end
–CAR FACTORY
function makeCars(params) – produces car objects
if params.orientation == “vertical” then
aCar = new({x = spawnX, y = spawnY})
physics.addBody(aCar)
aCar.isSensor = true --object does not respond to collision with Corona’s in-build physics engine, but still detects collision.
aCar.moveCar()
aCar:addEventListener(“collision”, onLocalCollision)
Runtime:addEventListener(“enterFrame”, destroyCars)
end
if params.orientation == “horizontal” then
bCar = new({x = spawn2X, y = spawn2Y})
bCar:rotate(270)
physics.addBody(bCar)
bCar.isSensor = true
bCar.moveCar()
bCar:addEventListener(“collision”, car.onLocalCollision)
Runtime:addEventListener(“enterFrame”, destroyCars)
end
end
–CAR SPAWNER
function spawnCars(event) – decide when to spawn a new car
math.randomseed(os.time()) --seed math.random so it generates numbers according to the CPU timer, making results more random.
local random = math.random(math.abs(5), math.abs(10)) * 10 --results in tens.
local random2 = math.random(math.abs(10), math.abs(70)) * 10
if _G.once == false then
makeCars({orientation = “vertical”})
makeCars({orientation = “horizontal”})
_G.once = true
end
if (aCar) and _G.endGame == false and _G.once == true then
if aCar.y > spawnY + random then
makeCars({orientation = “vertical”})
end
if bCar.x > spawn2X + random2 then
makeCars({orientation = “horizontal”})
end
end
end
--------------------------------------------[/lua]
menu.lua:
[lua]module(…, package.seeall)
new = function(params)
local menuGroup = display.newGroup()
local setup = require(“setup”)
local director = require(“director”)
function displayMenuBack()
local menuBack = display.newImage(“viper.jpeg”)
menuGroup:insert(menuBack)
end
local function tap(event)
_G.once = false
_G.endGame = false
director:changeScene(“level1”, “moveFromLeft”)
return true
end
startButton:addEventListener(“tap”, tap)
return menuGroup
end[/lua]
level1.lua:
[lua]module(…, package.seeall)
new = function (params)
–INCLUDE FILES
local ui = require(“ui”)
local setup = require(“setup”)
local car = require(“car”)
local physics = require(“physics”)
local ped = require(“ped”)
local director = require(“director”)
–SETUP PHYSICS
…
–GLOBALS
…
–DISPLAY NON-OBJECT ITEMS
…
function unloadGame(event)
_G.endGame = true
director:changeScene(“menu”)
end
backButton:addEventListener(“tap”, unloadGame)
Runtime:addEventListener(“enterFrame”, car.spawnCars)
return localGroup
end[/lua]
main.lua
[lua]------------------------------------------------------------
–main.lua
–INCLUDE FILES
local ui = require(“ui”)
local setup = require(“setup”)
local car = require(“car”)
local physics = require(“physics”)
local stp = require(“stp”)
local ped = require(“ped”)
local director = require(“director”)
require(“ice”)
–SETUP PHYSICS
physics.start()
once = false
endGame = false
highScores = nil
highScores = ice:loadBox(“highScores”)
local mainGroup = display.newGroup()
local function main()
mainGroup:insert(director.directorView)
director:changeScene(“menu”)
return true
end
main()[/lua]
and the error:
[lua]Runtime error
…ers/nikos/Documents/workspace/TrafficControl/car.lua:67: attempt to call field ‘removeBody’ (a nil value)
stack traceback:
[C]: in function ‘removeBody’
…ers/me/Documents/workspace/project/car.lua:65: in function <…ers>
?: in function <?:215>[/lua]
which translates to line 50 of car.lua, as I have removed irrelevant variables and functions.
What am I doing wrong? [import]uid: 106739 topic_id: 21263 reply_id: 321263[/import] </…ers>