【问题标题】:Love2D how to use a string to add a textureLove2D如何使用字符串添加纹理
【发布时间】:2019-10-24 10:55:31
【问题描述】:

这不是最好的标题,是的,但我什至不知道如何总结这个问题。我试图“导出”纹理,它会像“污垢”或其他东西,然后一旦你输入纹理,它就会被添加到屏幕上。我已经有了纹理,我需要知道如何使用字符串来添加纹理。我希望这不是太难阅读和理解。

【问题讨论】:

  • 当您说export 然后谈论adding to the screen 时,我们是在谈论创建纹理文件输出然后加载该文件以将其显示在屏幕上吗?如果不是,我们要导出什么?

标签: lua love2d


【解决方案1】:

您所说的“导出”有点令人困惑。以下代码 sn-p 将可重复使用的纹理图像存储在 textures 中,将要绘制的纹理存储在 onScreen 中。请注意,我们将textures 表用作键值字典,而将onScreen 表用作有序列表。

function love.load()
    textures = {} -- creates a table to store textures
    textures['dirt'] = love.graphics.newImage('dirt.png') -- adds the 'dirt' texture, with a dictionary-like key

    onScreen = {} -- creates a table to store what's drawn on screen
    onScreen[#onScreen + 1] = {'dirt', 0, 0} -- appends the 'dirt' name and coords for drawing to the list of textures being drawn
end

function love.draw()
    for ind, val in pairs(onScreen) do -- for all textures onScreen
        love.graphics.draw(textures[val[1]], val[2], val[3]) -- get the image for the texture name and draw at coords
    end
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2017-11-24
    • 1970-01-01
    • 2011-06-16
    相关资源
    最近更新 更多