[Solved] Empty space in tile grid?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
[Solved] Empty space in tile grid?
Hi, I was wondering if there was a way to have an empty space in a tile grid without using a transparent tile? I would like to have an empty grid, and just place tiles where I would want platforms, since I'm making a platformer.
Last edited by Ryne on Sat Oct 23, 2010 3:55 am, edited 1 time in total.
@rynesaur
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Empty space in tile grid?
Sure. It depends a lot on your code, though. Are you using the tile tutorial?
If so, then you must assign one of the 'numbers' of the map to the empty tile. Assuming that you assign them to 0, you can do this:
If so, then you must assign one of the 'numbers' of the map to the empty tile. Assuming that you assign them to 0, you can do this:
Code: Select all
function draw_map()
for y=1, map_display_h do
for x=1, map_display_w do
if map[y+map_y][x+map_x] ~= 0 -- transparent tiles (0) are ignored
love.graphics.draw(
tile[map[y+map_y][x+map_x]],
(x*tile_w)+map_offset_x,
(y*tile_h)+map_offset_y )
end
end
end
end
When I write def I mean function.
Re: Empty space in tile grid?
Great!, thanks again . I might as well ask here before posting another thread, but I would like to spawn a player sprite into a grid coordinate. I'm not sure if that is as easy as it sounds. For example. To draw an animated sprite at a screen coordinate I would use this code:
My first instinct was to try and place the unit into the grid using
or
but apparently neither of these work. I was also thinking of a more complicated way that would involve converting grid coordinates into screen coordinates but I'm unsure of how to go about doing it. Any Ideas?
Code: Select all
anim:draw(100,329)
Code: Select all
anim:draw(map[1], map[3])
Code: Select all
anim:draw(#map[1], #map[3])
@rynesaur
Re: Empty space in tile grid?
Shouldn't you multiply the position by the tilesize, then subtract the camera?Ryne wrote:Great!, thanks again . I might as well ask here before posting another thread, but I would like to spawn a player sprite into a grid coordinate. I'm not sure if that is as easy as it sounds. For example. To draw an animated sprite at a screen coordinate I would use this code:My first instinct was to try and place the unit into the grid usingCode: Select all
anim:draw(100,329)
orCode: Select all
anim:draw(map[1], map[3])
but apparently neither of these work. I was also thinking of a more complicated way that would involve converting grid coordinates into screen coordinates but I'm unsure of how to go about doing it. Any Ideas?Code: Select all
anim:draw(#map[1], #map[3])
Hello, I am not dead.
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Empty space in tile grid?
Usually a mapToWorld and a worldToMap function would be quite useful in general. I'm a bit surprised that the tutorial itself doesn't include that.
Coding real quick and without testing anything at all:
Then you can use them like this:
You can actually use mapToWorld to simplify the drawing loop:
Now, to answer Ryne's question, in order to draw an animation in cell {3,10} he could do:
Again, I warn you that the code above is completely untested.
PS: "world", not "screen". In the future you might want to add scrolling.
Coding real quick and without testing anything at all:
Code: Select all
function mapToWorld(x,y)
return (x*tile_w)+map_offset_x, (y*tile_h)+map_offset_y )
end
function worldToMap(x,y)
return math.floor((x - map_offset_x) / tile_w), math.floor((y - map_offset_y) / tile_y)
end
Code: Select all
-- transform the coordinates of cell 1,4 into world coordinates
local x,y = mapToWorld(1,4)
-- get the indexes of the cell that holds the coordinates x=200, y=300
local cx,cy = worldToMap(200, 300)
Code: Select all
function draw_map()
for y=1, map_display_h do
for x=1, map_display_w do
if map[y+map_y][x+map_x] ~= 0 -- transparent tiles (0) are ignored
love.graphics.draw( tile[map[y+map_y][x+map_x]], mapToWorld(x,y)) -- calculations are done inside mapToWorld
end
end
end
end
Code: Select all
anim:draw(mapToWorld(3,10))
PS: "world", not "screen". In the future you might want to add scrolling.
When I write def I mean function.
Re: Empty space in tile grid?
Alright, that makes a lot of sense, thanks a lot for the help guys. I'll post my project once I get a little more finished.kikito wrote:Usually a mapToWorld and a worldToMap function would be quite useful in general. I'm a bit surprised that the tutorial itself doesn't include that.
@rynesaur
Who is online
Users browsing this forum: No registered users and 68 guests