首页 > 解决方案 > 使用 LOVE2D 制作平台游戏 - 垂直关卡在某个点之后没有碰撞

问题描述

对于在线课程,我正在使用 LÖVE2D 和 lua 创建一个平台游戏。关卡是由瓷砖生成的——普通水平的瓷砖垂直填充,垂直水平的水平填充——这些瓷砖的 ID 对应于它们是否是实心的。然而,在垂直关卡中,在某个点之后,应该可以碰撞的图块变成不可碰撞的。(可以在这里看到)

我有一种感觉,它与瓦片地图及其创建方式有关。我尝试过改变关卡的宽度,这就是我发现我为垂直关卡设置的宽度决定了碰撞被切断的位置。例如,如果关卡宽度为 16 格,则碰撞向下 16 格结束,如果关卡宽度为 10 格,则碰撞向下 10 格结束。

我还没弄清楚是什么原因造成的。谁能帮我解决这个问题?这是我的瓷砖地图代码:

TileMap = Class{}

function TileMap:init(width, height, orientation)
    self.width = width
    self.height = height
    self.tiles = {}
    self.orientation = orientation
end

--[[
    If our tiles were animated, this is potentially where we could iterate over all of them
    and update either per-tile or per-map animations for appropriately flagged tiles!
]]
function TileMap:update(dt)

end

--[[
    Returns the x, y of a tile given an x, y of coordinates in the world space.
]]
function TileMap:pointToTile(x, y)
    if self.orientation == 'horizontal' then
        if x < 0 or x > self.width * TILE_SIZE or y < 0 or y > self.height * TILE_SIZE then
            return nil
        end
    
        return self.tiles[math.floor(y / TILE_SIZE) + 1][math.floor(x / TILE_SIZE) + 1]
    elseif self.orientation == 'vertical' then
        if x < 0 or x > self.width * TILE_SIZE or y < 0 or y > self.height * TILE_SIZE then
            return nil
        end
        return self.tiles[math.floor(x / TILE_SIZE) + 1][math.floor(y / TILE_SIZE) + 1]
    end
end

function TileMap:render()
    for y = 1, self.height do
        for x = 1, self.width do
            self.tiles[y][x]:render()
        end
    end
end

这是我的垂直关卡制作工具:

VertLevelMaker = Class{}

function VertLevelMaker.generate(height, width)
    local tiles = {}
    local entities = {}
    local objects = {}

    local tileID = TILE_ID_GROUND
    
    -- whether we should draw our tiles with toppers
    local topper = true
    local tileset = math.random(20)
    local topperset = math.random(20)
    
    for y = 1, width do
        table.insert(tiles, {})
    end
    
    local leftPlat = height % 8
    local rightPlat = height % 8 + 4
    -- place all the ground as you go down
    for y = 1, 10 do
        tileID = TILE_ID_EMPTY
        for x = 1, width do
            table.insert(tiles[x], 
                Tile(x, y, tileID, nil, tileset, topperset))
        end
    end
    
    tileID = TILE_ID_GROUND
    for y = 11, height - 1 do
        table.insert(tiles[1],
            Tile(1, y, tileID, y == 11 and topper or nil, tileset, topperset))
        if leftPlat >= 8 then
            leftPlat = leftPlat % 8
            for x = 2, 6 do
                table.insert(tiles[x],
                    Tile(x, y, tileID, y == y and topper or nil, tileset, topperset))
            end
            tileID = TILE_ID_EMPTY
            for x = 7, width - 1 do
                table.insert(tiles[x],
                    Tile(x, y, tileID, nil, tileset, topperset))
            end
        elseif rightPlat >= 8 then
            rightPlat = rightPlat % 8
            tileID = TILE_ID_EMPTY
            for x = 2, width - 6 do
                table.insert(tiles[x],
                    Tile(x, y, tileID, nil, tileset, topperset))
            end
            tileID = TILE_ID_GROUND
            for x = width - 5, width - 1 do
                table.insert(tiles[x],
                    Tile(x, y, tileID, y == y and topper or nil, tileset, topperset))
            end
        else
            tileID = TILE_ID_EMPTY
            for x = 2, width - 1 do
                table.insert(tiles[x],
                    Tile(x, y, tileID, nil, tileset, topperset))
            end
        end
        tileID = TILE_ID_GROUND
        table.insert(tiles[width],
            Tile(width, y, tileID, y == 11 and topper or nil, tileset, topperset))
        leftPlat = leftPlat + 1
        rightPlat = rightPlat + 1
    end
    for x = 1, width do
        table.insert(tiles[x], 
            Tile(x, height, tileID, topper, tileset, topperset))
    end
    
    local map = TileMap(height, width, 'vertical')
    map.tiles = tiles
    
    return Level(entities, objects, map, width, height)
end

(对不起,如果不是最好的措辞,这是我在这里发布的第一个问题)(另外,在你问之前,高度和宽度是故意向后放入瓦片地图的,否则它不起作用)

标签: lua2dlove2d

解决方案


我已经解决了!问题不在于瓷砖的状态,而在于它们的排列方式。在垂直关卡生成器中更改了瓦片表的结构后,瓦片被水平排序,我只需要调整瓦片地图中的 pointToTile 函数以匹配它。


推荐阅读