Tile Map help

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.
coke905
Citizen
Posts: 52
Joined: Tue Jun 05, 2012 1:46 am

Tile Map help

Post by coke905 »

Im new to love and im using 8.0.

I'd like to make a top down rpg game, rpg elements will be added in later ofcourse. The problem that im running into is sprite animation and tilemapping.
I've looked in google but i could only find 3 tile map examples so i tried them but it didn't seem to work with 8.0 so now im confused. If anyone has TileMap examples please respond to this.

Thanks everyone.
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: Tile Map help

Post by coffee »

coke905 wrote:Im new to love and im using 8.0.

I'd like to make a top down rpg game, rpg elements will be added in later ofcourse. The problem that im running into is sprite animation and tilemapping.
I've looked in google but i could only find 3 tile map examples so i tried them but it didn't seem to work with 8.0 so now im confused. If anyone has TileMap examples please respond to this.

Thanks everyone.
You searched in Google? Why? What's wrong with forum search? you get plenty of hits on the matter!
And in WIKI you have some tutorials for properly start with games related with tilemaps:
https://love2d.org/wiki/Category:Tutorials
coke905
Citizen
Posts: 52
Joined: Tue Jun 05, 2012 1:46 am

Re: Tile Map help

Post by coke905 »

Thanks but that didn't really help ive already looked at those pages and it still doenst work with my LOVE 8.0. Im always getting errors in the draw event in it and honestly i dont know what each thing does so i was wondering if anyone has a tutorial on it. maybe video or something
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: Tile Map help

Post by coffee »

coke905 wrote:Thanks but that didn't really help ive already looked at those pages and it still doenst work with my LOVE 8.0. Im always getting errors in the draw event in it and honestly i dont know what each thing does so i was wondering if anyone has a tutorial on it. maybe video or something
Seems you probably don't have a tilemap problem but after all a generic 0.8 conversion problem. You need to be more objective describing your errors, you can't expect that we guess what errors those are. So please type the errors . Even better and as we recommend in forum rules post your love file. That way we can much faster understand your problems.

Things usually work well and almost the same using 0.7 code in 0.8. However minor alterations may be required. A common problem is the old way how we still did linkage to other lua files like loading files in "require" (you should use "namefile" instead of "namefile.lua"). However you tell that is a draw error so back to start, post your error messages or even better your love file.
coke905
Citizen
Posts: 52
Joined: Tue Jun 05, 2012 1:46 am

Re: Tile Map help

Post by coke905 »

function Init_Map()
map1 = {0,0,0,0,0,0,0,0}

tile = {}

for i=1,8 do -- change 3 to the number of tile images minus 1.
tile = love.graphics.newImage( "Images/tile"..i..".png" )
end


-- map variables
map1_display_w = 14
map1_display_h = 10


end

function Draw_Map()
for y=0, map1_display_h do
for x=0, map1_display_w do
love.graphics.draw(tile[map1], x * 32, y * 32)
end
end
end



.. this is my Map1.lua file everything but the function Draw_Map() is working. i followed the wiki guide and its not working. atm its not the exact same as the wiki guide but in general it is. I've tried to do exactly what wiki said but it didnt work either so i guess im experementing. Anyway here is my Character class
and im getting errors at love.graphics.draw(tile[map1], x * 32, y * 32)
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Tile Map help

Post by Roland_Yonaba »

Hi coke905...

Wrap your snippets with code tags, as it makes your posts easier to read.
Now, consider one thing.

Code: Select all

love.graphics.draw(tile[map1], x * 32, y * 32)
Writing this will make Lua look inside table "tile" for a key named "map1".
Fact is there is no such thing inside table "tile".

Code: Select all

for i=1,8 do -- change 3 to the number of tile images minus 1.
tile[i] = love.graphics.newImage( "Images/tile"..i..".png" )
end
Table "tile" is keyed like this:

Code: Select all

Key "1" --> image
Key "2" --> image
Key "2" --> image
...
Key "8" --> image
I don't know exactly what example on the wiki you're trying to follow, but basically, that should work.
1. create a table to store your images, like "tile" table

Code: Select all

for i=1,8 do
   tile[i] = love.graphics.newImage( "Images/tile"..i..".png" )
end
2. create another table, 1D or 2D, as you wish. Each number inside that table will point directly to the corresponding image key inside "tile" table.

Code: Select all

local map = {
   {1,1,1,1,2,2,3,3,8},
   {1,1,1,1,2,2,3,3,8},
   {1,1,1,1,2,2,3,3,8},
   {1,1,1,1,2,2,3,3,8},
}
3. Then, draw !

Code: Select all

for y=1, #map do
   for x=1, #map[1] do 
   love.graphics.draw(tile[map[y][x]], x * tileWidth, y * tileHeight)
   end
end
Adapt it and that should work!
If you get errors, post your *.love file.
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: Tile Map help

Post by coffee »

Is important to say that Wiki tutorials are made regularly by a lot of new users and they work. Also that the issue was not at all a 0.7/0.8 problem.

Yonaba was kind enough to teach you tilemaping but didn't exactly said what was failing in your code. I will:
1 - Your map1 was too "short". map1_display_w and map1_display_h demanded that your map1 be at least one table 14x10. As it was "one-dimensional" would always made error if you didn't fill the table with required data or adjust the map1_display_w and map1_display_h to 7 and 1 (your map1 real size).
2 - Your x,y draw cycle should start at 1 and not zero because indexed data stored in Lua tables don't start by default at 0. Your draw routine was asking for data that didn't exist.

Your code with properly changes and put in Love cycles actually work well:

Code: Select all

function love.load()

   -- map variables
   map1_display_w = 14
   map1_display_h = 10

   Init_Map()

end

function love.update(dt)

end

function love.draw()

	Draw_Map()

end

function Init_Map()

	map1 = {
		{1,1,1,1,1,1,1,1,1,1,1,1,1,1},
		{1,1,1,1,1,1,1,1,1,1,1,1,1,1},
		{1,1,1,1,1,1,1,1,1,1,1,1,1,1},
		{1,1,1,1,1,1,1,1,1,1,1,1,1,1},
		{1,1,1,1,1,1,1,1,1,1,1,1,1,1},
		{1,1,1,1,1,1,1,1,1,1,1,1,1,1},
		{1,1,1,1,1,1,1,1,1,1,1,1,1,1},
		{1,1,1,1,1,1,1,1,1,1,1,1,1,1},
		{1,1,1,1,1,1,1,1,1,1,1,1,1,1},
		{1,1,1,1,1,1,1,1,1,1,1,1,1,1}
	}
	
	tile = {}

	for i=1,8 do -- change 3 to the number of tile images minus 1.
		tile[i] = love.graphics.newImage( "Images/tile"..i..".png" )
	end

end

function Draw_Map()
	for y=1, map1_display_h do
		for x=1, map1_display_w do 
			love.graphics.draw(tile[map1[y][x]], x * 32, y * 32)
		end
	end
end
coke905
Citizen
Posts: 52
Joined: Tue Jun 05, 2012 1:46 am

Re: Tile Map help

Post by coke905 »

Wow thanks for your help, i figured out why non of it worked in my draw method i kept saying for y = 0. so i set that to one but now im having another problem

when i set x and y = 1 in the draw method it draws it 32 pixels right and 32 pixels down so i have giant squares missing in between anyone know how to fix this ?
coke905
Citizen
Posts: 52
Joined: Tue Jun 05, 2012 1:46 am

Re: Tile Map help

Post by coke905 »

Oh and once someone tells me how to fix that i got collision function but id like to make it so my character cant walk through a rectangle that i draw there. So i was wondering how wud i do that if i set Player.x = 0 then my character just teleports to the top left corner and if i do Player.x = Player.x + 0 it doesnt do anything. Im calling my function after the playermovement shud i be calling it before ?
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Tile Map help

Post by Roland_Yonaba »

I'm unsure, maybe you should post your source.
Guessing you're using coffee's snippet as a base-code, take into account that setting x,y at 1 results in this (obviously)

Code: Select all

y=1,x=1 --> draw(32,32)
y=1,x=2 --> draw(32*2,32)
...
y=2,x=1 --> draw(32,32*2)
y=2,x=2 --> draw(32*2,32*2)
...
Maybe you should substract 1 from the local iterating value to have things working as expected.

Code: Select all

function Draw_Map()
   for y=1, map1_display_h do
      for x=1, map1_display_w do 
         love.graphics.draw(tile[map1[y][x]],( x-1) * 32, (y-1) * 32)
      end
   end
end
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 7 guests