A loop within a loop

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
Ihazard
Prole
Posts: 2
Joined: Fri Dec 09, 2011 10:21 pm

A loop within a loop

Post 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?
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: A loop within a loop

Post by tentus »

That code looks fine to me. Could you provide us with some more context, please?
Kurosuke needs beta testers
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: A loop within a loop

Post by Robin »

See the rules, please upload a .love.

And at the very least, tell us the error message and where it points to.
Help us help you: attach a .love.
Ihazard
Prole
Posts: 2
Joined: Fri Dec 09, 2011 10:21 pm

Re: A loop within a loop

Post 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
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: A loop within a loop

Post 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.
Kurosuke needs beta testers
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: A loop within a loop

Post by MarekkPie »

If you don't know how to make a .love file...

https://love2d.org/wiki/Game_Distribution
User avatar
IMP1
Prole
Posts: 43
Joined: Mon Oct 03, 2011 8:46 pm

Re: A loop within a loop

Post 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).
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: A loop within a loop

Post 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).
Post Reply

Who is online

Users browsing this forum: No registered users and 202 guests