What is the easiest way to draw a tile map imported from Tiled?

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
MaxGamz
Party member
Posts: 107
Joined: Fri Oct 28, 2022 3:09 am

What is the easiest way to draw a tile map imported from Tiled?

Post by MaxGamz »

I was watching a tutorial related to making a platformer and it explained the collisions but not actually drawing the tile images in the right places. I am aware that I need a nested loop and I don't want to use the STI library.
MaxGamz
Party member
Posts: 107
Joined: Fri Oct 28, 2022 3:09 am

Re: What is the easiest way to draw a tile map imported from Tiled?

Post by MaxGamz »

MaxGamz wrote: Sat Sep 02, 2023 11:36 am I was watching a tutorial related to making a platformer and it explained the collisions but not actually drawing the tile images in the right places. I am aware that I need a nested loop and I don't want to use the STI library.
Ok I found something that might help but the code is still giving me a lot of errors, I feel like the problem might have to do with the indexing, but when I tweaked it it had a problem with the quads

Code: Select all

function LoadTiledMap(path)
    local level = require("Map/Level_1")

    level.quads = {}
    local tileset = level.tilesets[1]
    level.tileset = tileset


    level.image = love.graphics.newImage(tileset.image)

    for y = 0, (tileset.imageheight / tileset.tileheight) - 1 do 
        for x = 0, (tileset.imagewidth / tileset.tilewidth) - 1 do
            local quad = love.graphics.newQuad(
                x * tileset.tilewidth,
                y * tileset.tileheight,
                tileset.tilewidth,
                tileset.tileheight,
                tileset.imagewidth,
                tileset.imageheight

            )
            table.insert(level.quads, quad)
        end
    end

    function level:draw()
        for i, layer in ipairs(self.layers) do
            for y = 0, layer.height - 1 do -- errors arrise here because of indexing but even after fixing it, it cant draw it
                for x = 0, layer.width - 1 do
                    local index = (x + y * layer.width) + 1
                    local tid = layer.data[index]

                    if tid ~= 0 then
                        local quad = self.quads[tid]
                        local xx = x * self.tileset.tilewidth
                        local yy = y * self.tileset.tileheight
                        love.graphics.draw(self.image, quad, xx, yy) -- error arises because it says  that quad has a nil value
                    end
                end
            end
        end
    end

    return level
end
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 11 guests