lua - 尝试在 Lua/Love2D 中绘制平台时出错
问题描述
这里是 Lua/Game dev 的新手。我正在尝试编写代码来绘制我的游戏背景。对于与设置相关的事情,我有四个不同的精灵表:
- 背景 - 包含绘制的游戏“背景图像”
- 平台 - 包含游戏的平台精灵
- objectsBig - 包含背景中相对较大的绘制对象(例如岩石、海藻等)
- objectsSmall - 包含相对较小的绘制对象(较小的岩石、植物等)
我创建了一个函数,generateQuads()
在我的Util.lua
文件中调用它,给定一个 spritesheet,将它拼接成 spritesheet 中的不同 sprite 并返回这些 sprites。如下:
-- 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 的平台精灵表,其中每个 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
。
解决方案
推荐阅读
- c# - 进程类启动泄漏内存
- c# - “使用 Visual Studio 调试 lambda 表达式”不再起作用?
- android - SIMpleDateFormat 使用 GMT + 6.5 返回错误值
- swift4 - 是否可以使用 ML Kit 以纵向模式扫描条码?
- ruby - 使用 Ruby 为 iTunes Connect API 生成 JWT
- c++ - operator< 给出 operator __surrogate_func 错误
- gradle - 如何有条件地接受 Kotlin DSL 中的 Gradle 构建扫描插件服务条款?
- sql - 如何获得多个 SUM 语句的总数
- java - 如何在我的 java 应用程序中连接到 mongo db?
- c# - 在 ASP.net 核心应用程序的强类型配置类中使用 System.Uri