iteration problem

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

iteration problem

Post by Ryne »

So before I was drawing trees based on what number was in the tile grid. Which worked great, but (like Jasoco said) I'd like to have each tree as it's own object.

I've managed to come up with this, which works fine If I wasn't to place EVERY tree individually in load(), but I don't. I'd like for the trees to still spawn based on the number of the tile.

Code: Select all

trees = {}

function spawnTrees(x, y, w, h, t)
	
	table.insert(trees, {x = x, y = y, w = w, h = h, t = t})

end

function drawTrees()

	for i=1, #trees do			 
		love.graphics.drawq(images.tileset, quads[trees[i].t], mapToWorld(trees[i].x, trees[i].y))
	end

end

this was my old code

Code: Select all

function drawTrees()

	for i = 1, #map do
		for l = 1, #map do
			if map[i][l] ~= 0 then
			love.graphics.drawq(images.tileset, quads[map[i][l]], mapToWorld(l-1, i-2))
			end
		end
	end

end
I tried to use the same iteration in the old code, but I couldn't get it to work properly. Any Ideas?
@rynesaur
User avatar
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Re: iteration problem

Post by Ryne »

turns out I solved it by doing

Code: Select all

trees = {}

function spawnTrees()

   for i = 1, #map do
      for l = 1, #map do
         if map[i][l] ~= 0 then
		table.insert(trees, {t = quads[map[i][l]], x = l-1, y = i-2})
         end
      end
   end

end

function drawTrees()

	for i=1, #trees do			 
		love.graphics.drawq(images.tileset, trees[i].t, mapToWorld(trees[i].x, trees[i].y))
	end

end
incase anyone finds this useful.
@rynesaur
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 47 guests