尝试在Lua / Love2D中绘制平台时出错

问题描述

这里一般是Lua / Game开发人员的新手。我正在尝试编写代码来绘制游戏背景。我有四个不同的Spritesheets与设置有关:

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

我在我的generateQuads()文件中创建了一个名为Util.lua函数,给定一个spritesheet,将其拼接为spritesheet中的不同sprite,然后返回这些sprite。如下:

-- 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,tileHeight,atlas:getDimensions())
        sheetCounter = sheetCounter + 1
    end
end

return quads

end

我还有一个名为Map的类,该类旨在包含有关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,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,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,(x - 1) * self.platformWidth,(y - 1) * self.platformHeight)
        end
    end
end

我想做的是拼接每个Spritesheet,并设置背景和底部图块,以便可以看到底部。我正在尝试通过为Spritesheet中的组件赋予数字来实现此目的,例如,在1500x500 Spritesheet中,将整个背景视为“一个” Sprite,并进行了同样的处理。其数字1在变量中给出。另一个示例是一个240x30平台的Spritesheet,其中每个60x30都被视为“一个” Sprite,并指定了相应的编号,如下所示:

-- 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,GROUND_MIDDLE)
    end

love.graphics.draw(self.platform,(y - 1) * self.platformHeight)

行,该程序将无法运行。

感谢您的帮助!

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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)