Tile-map Example 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.
Post Reply
User avatar
Kumlin
Prole
Posts: 6
Joined: Wed Sep 04, 2013 11:53 am

Tile-map Example help

Post by Kumlin »

Below is my code modified off of the wiki tutorial I cannot use the arrows keys to move my map around I have no idea what I did wrong :/

Code: Select all

function love.load()
	tile = {}
	tile [0] = love.graphics.newImage("perfectHex.jpg")

	love.graphics.setBackgroundColor(255, 255, 255)

	mapW = 132
	mapH = 132
	mapX = 0
	mapY = 0
	mapOffSetX = 30
	mapOffSetY = 30
	mapDisplayW = 14
	mapDisplayH = 10
	tileW = 128
	tileH = 128

	map={
	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
   	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
	}

end


function draw_map()
	for y=1, mapDisplayH do
		for x=1, mapDisplayW do
			love.graphics.draw(
				tile[map[y+mapY][x+mapX]],
				(x*tileW)+mapOffSetX,
				(y*tileH)+mapOffSetY)
		end
	end
end

function love.keypressed(key, unicode)
	if key == 'up' then 
		mapY = mapY - 1
		if mapY < 0 then mapY = 0; end
	end
	if key == 'down' then
		mapY = mapY + 1
		if mapY > mapH - mapDisplayH then mapY = mapH - mapDisplayH; end
	end

	if key == 'left' then
		mapX = math.max(mapX - 1, 0)
	end
	if key == 'right' then
		mapX = math.min(mapX+1, mapW - mapDisplayW)
	end
end

function love.draw()
	draw_map()
end
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Tile-map Example help

Post by raidho36 »

If the whole screen is tiled with exactly the same tile, and if the keypress would shift the view exactly by a tile dimension, then you wouldn't notice any difference even if it actually does moves.

Try printing grid coordinates on top of all other graphics to see if it's just you.
User avatar
Kumlin
Prole
Posts: 6
Joined: Wed Sep 04, 2013 11:53 am

Re: Tile-map Example help

Post by Kumlin »

added this line of code under draw_map()

Code: Select all

love.graphics.print(mapY, 100, 100, 0, 1, 1)
And it prints nothing also I would expect to see the tiles moving even if they were all the same I'd give you the file but it says the board quota has been reached so yeah...
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Tile-map Example help

Post by raidho36 »

You can upload your files to third party file hosting websites e.g. Google Drive. It offers 15 GB for free. There's also variety of dropboxes and whatnot. Just don't use filthy trash like Mega or Turbobit.

Due to how this demo works, tiles wouldn't look like they move, they instead "swap around" in the direction you pressed, so if all tiles are the same you won't see anything going on.

That snippet you posted should've printed a mapY at 100x100 screen coordinates, unless it's nil, which is by definition false because the code doesn't fails in the places where there's math operations on mapY. I suppose you did it wrong. Are you sure it's in love.draw and underneath the map_draw? There might be a problem that it prints it in white and it blends into the background LOL.
User avatar
Kumlin
Prole
Posts: 6
Joined: Wed Sep 04, 2013 11:53 am

Re: Tile-map Example help

Post by Kumlin »

Thanks for your help I got it working now! Pretty new too lua so I sometimes get lost :/
Post Reply

Who is online

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