【问题标题】:Error Trying to Draw Platforms in Lua/Love2D尝试在 Lua/Love2D 中绘制平台时出错
【发布时间】:2022-05-01 18:45:22
【问题描述】:

这里是 Lua/游戏开发的新手。我正在尝试编写代码来绘制我的游戏背景。对于与设置相关的事情,我有四个不同的精灵表:

  • 背景 - 包​​含绘制的游戏“背景图像”
  • 平台 - 包含游戏的平台精灵
  • objectsBig - 包含背景中相对较大的绘制对象(例如岩石、海藻等)
  • objectsSmall - 包含相对较小的绘制对象(较小的岩石、植物等)

我创建了一个名为generateQuads() 的函数,该函数位于我的Util.lua 文件中,给定一个精灵表,它将它拼接成精灵表中的不同精灵并返回这些精灵。如下:

-- Function to generate quads/cut the spritesheet
function generateQuads(atlas, tileWidth, tileHeight)
    local sheetWidth = atlas:getWidth() / tileWidth
    local sheetHeight = atlas:getHeight() / tileHeight
local sheetCounter = 1
local quads = {}

-- for every line of the sprite sheet
for y = 0, sheetHeight - 1 do
    -- for every piece in the width
    for x = 0, sheetWidth - 1 do
        -- quads generated will be the ones specified by tileWidth and tileHeight
        quads[sheetCounter] = love.graphics.newQuad(x * tileWidth, y * tileHeight, tileWidth, 
            tileHeight, atlas:getDimensions())
        sheetCounter = sheetCounter + 1
    end
end

return quads

end

我还有一个名为Map 的类,用于包含地图的信息。以下是其中的功能:

设置类的功能

function Map:init()
    -- Load the background sprite into variable
    -- Each is 500x500, total is 1500x500
    self.background = love.graphics.newImage('Graphics/platform/background/full.png')

-- Load all the ground platforms into variable
-- Each is 60x30, total is 240x30
self.platforms = love.graphics.newImage('Graphics/platform/ground/all.png')

-- Load all the bigger misc objects into variable
-- Each is 15x20, total is 15x120
self.objectsBig = love.graphics.newImage('Graphics/platform/objects/15x20/all.png')

-- Load all the smaller misc objects into variable
-- Each is 15x15, total is 60x15
self.objectsSmall = love.graphics.newImage('Graphics/platform/objects/15x15/all.png')

-- Setting the size for each of the variables IN PIXELS
self.backgroundWidth = 500
self.backgroundHeight = 1500

self.platformWidth = 60
self.platformHeight = 30

self.objectsBigWidth = 15
self.objectsBigHeight = 20

self.objectsSmallWidth = 15
self.objectsSmallHeight = 15

-- Setting the map size IN TILES
-- EXPERIMENTAL, CHECK AGAIN
self.mapWidth = 432
self.mapHeight = 243

-- Setting the width of the map IN PIXELS (in relation to platforms)
self.mapWidthPixels = self.backgroundWidth
self.mapHeightPixels = self.backgroundHeight

-- Storing our map features
self.tiles = {}

-- Storing the camera points, starting from the bottom
-- The 243 is the VIRTUAL_HEIGHT, which we needed to subtract to account for the
-- offset of the screen
self.camX = 0
self.camY = self.backgroundHeight - 243

-- Cutting each of the spritesheets
self.backgroundSprite = generateQuads(self.background, self.backgroundWidth, self.backgroundHeight)
self.platformSprite = generateQuads(self.platforms, self.platformWidth, self.platformHeight)
self.objectsBigSprite = generateQuads(self.objectsBig, self.objectsBigWidth, self.objectsBigHeight)
self.objectsSmallSprite = generateQuads(self.objectsSmall, self.objectsSmallWidth, self.objectsSmallHeight)

-- 'Setting' the tiles for the map to be background 
for y = 1, self.mapHeight do
    for x = 1, self.mapWidth do
        self:setTile(x, y, BACKGROUND)
    end
end

-- 'Setting' the platform tiles for where we start
for x = 1, self.mapWidth do
    self:setTile(x, self.mapHeight - 303, GROUND_MIDDLE)
end
end

在 X、Y 坐标中“设置”瓷砖应该在哪里的功能

function Map:setTile(x, y, tile)
    -- The table 'tiles' is going to store what tile should be in what x and y position
    -- subtracting y by 1 so that it's 0 indexed, not 1 indexed
    self.tiles[(y - 1) * self.mapWidth + x] = tile
end

返回 X、Y 坐标中的瓷砖的功能

function Map:getTile(x, y, tile)
    -- This function is going to tell us what tile is set at this x and y position
    return self.tiles[(y - 1) * self.mapWidth + x]
end

更新功能

function Map:update(dt)
    -- Moving the camera depending on the key being pressed
    if love.keyboard.isDown('w') then
        -- up movement, clamped between 0 and the other
        self.camY = math.max(0, math.floor(self.camY - SCROLL_SPEED * dt))
    elseif love.keyboard.isDown('a') then
        -- left movement
        self.camX = math.max(0, math.floor(self.camX - SCROLL_SPEED * dt))
    elseif love.keyboard.isDown('s') then
        -- down movement, subtracting VIRTUAL_HEIGHT to account for the screen offset
        self.camY = math.min(self.backgroundHeight - VIRTUAL_HEIGHT, math.floor(self.camY + SCROLL_SPEED * dt))
    elseif love.keyboard.isDown('d') then
        -- right movement
        self.camX = math.min(self.backgroundWidth - VIRTUAL_WIDTH, math.floor(self.camX + SCROLL_SPEED * dt))
    end
end

渲染函数

function Map:render()
    for y = 1, self.mapHeight do
        for x = 1, self.mapWidth do
            -- Drawing the background first
            love.graphics.draw(self.background, self.backgroundSprite[self:getTile(x, y)],
                (x - 1) * self.backgroundWidth, (y - 1) * self.backgroundHeight)
            love.graphics.draw(self.platform, self.platformSprite[self:getTile(x, y)],
                (x - 1) * self.platformWidth, (y - 1) * self.platformHeight)
        end
    end
end

我要做的是拼接每个精灵表,并设置背景和底部图块,以便我可以看到底层。我试图通过为我的精灵表中的组件提供数字来做到这一点,例如,在背景为 1500x500 的精灵表中,整个事物被认为是“一个”精灵,并被视为这样。它的数字 1 在变量中给出。另一个例子是一个 240x30 的平台 spritesheet,其中每个 60x30 被认为是“一个”精灵,并给定一个相应的数字,如下所示:

-- Know where the platforms are in the spritesheet
GROUND_LEFT = 1
GROUND_RIGHT = 2
GROUND_SMALL = 3
GROUND_MIDDLE = 4

-- Know where the backgrounds are in the spritesheet
BACKGROUND = 1

[...]

然后我想将每个存储在 self.tiles 列表中,以便我可以随时从表中访问它们(请注意,其中大部分来自 mario 的 CS50 实现,所以我明白如果我有任何概念错误)。但是,当我运行此代码时,出现以下错误:

Error

Error

Map.lua:135: bad argument #1 to 'draw' (Texture expected, got nil)


Traceback

[C]: in function 'draw'
Map.lua:135: in function 'render'
main.lua:48: in function 'draw'
[C]: in function 'xpcall'

本质上,我只是想知道一种设置底部平台图块的方法,然后能够遍历地图并“设置”其他对象精灵的位置。单独的背景效果很好,但是一旦我添加了:

-- 'Setting' the platform tiles for where we start
    for x = 1, self.mapWidth do
        self:setTile(x, self.mapHeight - 303, GROUND_MIDDLE)
    end

love.graphics.draw(self.platform, self.platformSprite[self:getTile(x, y)],
            (x - 1) * self.platformWidth, (y - 1) * self.platformHeight)

行,程序不会运行。

感谢任何帮助!

编辑:我遇到错误的那一行是self.platform

【问题讨论】:

  • Map.lua:135 是哪一行?是关于self.background 还是self.platform 的那个?
  • 这是 self.platform 之一!这就是我遇到的问题。
  • 好的,self.platformnil,所以你不能画它。你在哪里初始化字段platform?我在您的代码中没有看到这一点。
  • -- 'Setting' the platform tiles for where we start for x = 1, self.mapWidth do self:setTile(x, self.mapHeight - 303, GROUND_MIDDLE) end 不会初始化我想要的地图部分吗?
  • setTile 初始化 tiles,而不是 platform

标签: lua game-development love2d


【解决方案1】:

我之前也遇到过同样的问题,结果是在输入变量时出现了拼写错误。查看您的代码,我发现没有self.platform,而是有self.platforms

尝试:

love.graphics.draw(self.platforms, self.platformSprite[self:getTile(x, y)],
        (x - 1) * self.platformWidth, (y - 1) * self.platformHeight)

【讨论】:

    猜你喜欢
    • 2013-05-12
    • 1970-01-01
    • 2015-04-28
    • 2019-06-12
    • 2017-07-15
    • 2020-09-05
    • 1970-01-01
    • 2018-02-20
    • 2020-01-01
    相关资源
    最近更新 更多