Page 1 of 1

A loop within a loop

Posted: Mon Jan 16, 2012 2:04 pm
by Ihazard
I'm currently trying to draw tiles for my map but I keep getting an error when I try to do something like:

Code: Select all

for x = 1, 10 do
     for y = 1, 10 do
            if map[x][y] == 1 then love.graphics.draw( tile[1], ( x * tile_w ), (  y * tile_h )) end 
     end
end
Is using x and y as part of a loop acceptable, I know that it always works using i = 1, etc. but I just can't get it to work?

Re: A loop within a loop

Posted: Mon Jan 16, 2012 2:17 pm
by tentus
That code looks fine to me. Could you provide us with some more context, please?

Re: A loop within a loop

Posted: Mon Jan 16, 2012 3:24 pm
by Robin
See the rules, please upload a .love.

And at the very least, tell us the error message and where it points to.

Re: A loop within a loop

Posted: Mon Jan 16, 2012 9:00 pm
by Ihazard
It says that it has found a nil value in the line of code with the love.graphics.draw: this is the function that it's in, I tried to make it so that instead of looping through my MAPS table it just looked through the first one, but even that won't work..

Code: Select all

function DRAW_MAP()
	if GAME_INPLAY == 1 then
		if MAPS[1].chosen == 1 then 
				for x = 1, MAP_w do 
					for y = 1, MAP_h do
						if MAP_LEVEL[1][x][y] == 1 then
							love.graphics.draw(IMG_BLOCK_HOLDER, (x * tilewidth), (y * tileheight))
						end
					end
				end
		end
	end
end

Re: A loop within a loop

Posted: Mon Jan 16, 2012 9:10 pm
by tentus
Mostly likely IMG_BLOCK_HOLDER is incorrectly set, but again, we don't have enough to really help you. Please post a .love file.

Re: A loop within a loop

Posted: Mon Jan 16, 2012 9:44 pm
by MarekkPie
If you don't know how to make a .love file...

https://love2d.org/wiki/Game_Distribution

Re: A loop within a loop

Posted: Mon Jan 16, 2012 9:48 pm
by IMP1
In what format is the map?
I ask because when I have my maps, they're in the form

Code: Select all

{
  { 1, 1, 1, 1, 1, 1, 1 },
  { 1, 1, 1, 1, 1, 1, 1 },
  { 1, 1, 1, 1, 1, 1, 1 },
}
In which case the loop needs to be y and then x. And doing it the other way round (unless the map is square) will result in error (and even then it'll draw it rotated).

Re: A loop within a loop

Posted: Mon Jan 16, 2012 10:19 pm
by MarekkPie
IMP1 wrote:In what format is the map?
I ask because when I have my maps, they're in the form

Code: Select all

{
  { 1, 1, 1, 1, 1, 1, 1 },
  { 1, 1, 1, 1, 1, 1, 1 },
  { 1, 1, 1, 1, 1, 1, 1 },
}
In which case the loop needs to be y and then x. And doing it the other way round (unless the map is square) will result in error (and even then it'll draw it rotated).
Yeah, that almost always confuses me when changing from world coordinates to matrix coordinates. Matrix goes row, column (with the row being y, and the column being x).