Help With Tables? [Code Included]
Posted: Sun Dec 18, 2011 6:49 pm
I'm trying to make these clouds generate and slowly move throughout my map, but it won't work. I need each cloud to have an X and Y value, so I stuck each cloud in a table, and that table goes into the table that the script draws and moves. It keeps giving me "stack overflow" error, on the 'love.draw(v,v.X,v.Y)' so am I referring to the values in the table wrong? Note: All images exist, it's just the cloud part that is difficult. And yes, the cloud image exists. I've gotten the cloud to spawn from my second script, no problem, just I need to make multiple clouds generated and have them move. Code:
main.lua
cloudGen.lua
Any ideas? I'm new to this language.
main.lua
Code: Select all
require("cloudGen.lua")
cloudDraw = false
newcloud = 0
function love.load()
load_clouds()
island = love.graphics.newImage("SkyIsland.png",500,300)
tree = love.graphics.newImage("Tree.png",100,150)
bench = love.graphics.newImage("Bench.png",100,50)
end
function love.draw()
draw_clouds()
love.graphics.draw(island,100,200)
love.graphics.draw(tree,150,205)
love.graphics.setBackgroundColor(240, 248, 255)
end
function love.update()
newcloud = newcloud + 1
if newcloud >= 60 then
newcloud = 0
needCloud = true
end
end
Code: Select all
needCloud = false
clouds = {}
pendingClouds = {}
function load_clouds()
if needCloud == true then
needCloud = false
local cloud = love.graphics.newImage("Cloud.png",100,50)
local cloudTable = {}
table.insert(pendingClouds,cloudTable)
table.insert(cloudTable,cloud)
for i, v in pairs(pendingClouds) do
X = 500
Y = math.random(50,300)
table.insert(cloudTable,X)
table.insert(cloudTable,Y)
end
for i = 1,#pendingClouds do
table.insert(clouds,pendingClouds[i])
table.remove(pendingClouds,i)
end
end
end
function draw_clouds()
for i, v in pairs(clouds) do
love.draw(v.cloud,v.X,v.Y)--The "stack overflow" error says it's this line
v.X = v.X - 0.003
end
end