Hi,
I have some code in my game which spawns a car at a specified interval, the memory usage seems to creep up though even if its just the same event happening again and again.
Can anyone see a better way to perform the code?
Thanks much appreciated.
[blockcode]
local function trafficRight()
local function resetSpawn()
dospawnRight = 1
end
local function spawncar()
local cartype = 0
local chooseLane = math.random(3,4)
dospawnRight = 0
timer.performWithDelay(delayRight, resetSpawn, 1)
if level > 0 and level < 3 then
cartype = 1
elseif level == 3 then
cartype = 5
elseif level > 3 then
cartype = math.random(1,3)
end
if cartype == 1 then
local cpucar = display.newImageRect(settings.miniGreen, settings.miniGreen_H, settings.miniGreen_W)
physics.addBody(cpucar, “kinematic”, {isSensor = true })
if chooseLane == 3 then
cpucar.x = lane3x
else cpucar.x = lane4x
end
cpucar.dead = 0
cpucar.y = -50
cpucar.rotation = 90
cpucar.collision = onCollision
Runtime:addEventListener(“enterFrame”, cpucar )
cpucar:addEventListener(“collision”, cpucar )
function cpucar:enterFrame(event)
if cpucar.y > 480 then
cpucar.dead = 1
end
if pause == 0 then
if cpucar.dead == 1 then
Runtime:removeEventListener(“enterFrame”, cpucar)
cpucar:removeEventListener(“collision”, cpucar)
cpucar:removeSelf()
elseif cpucar.dead == 0 then
cpucar:setLinearVelocity(0,settings.gamespeed + math.random(60,80) )
end
else cpucar:setLinearVelocity(0,0)
end
end
end
end
if pause == 0 and level > 0 then
if dospawnRight == 1 then
spawncar()
end
end
end
timer.performWithDelay(100, trafficRight, 0 )
[/blockcode]
The full code has options for 2 more car types, but its the same code as above. [import]uid: 8699 topic_id: 6261 reply_id: 306261[/import]