lua - 图像不会使用多个文件在屏幕上绘制
问题描述
应该显示的图像没有显示。我将播放器代码和 main.lua 放在单独的文件中。这是我第一次在任何编程语言中使用多个文件
我试图创建一个矩形以确保它存在。矩形确实出现了,但图像没有。
this is player.lua
pl = { -- pl stands for player.
x = 100, -- player's x coordinate
y = 100, -- player's y coordinate
spd = 4, -- player's speed
dir = "", -- player's direction (n=north s=south e=east w=west
img = {
n = love.graphics.newImage("images/player/playern.png"),
s = love.graphics.newImage("images/player/players.png"),
e = love.graphics.newImage("images/player/playere.png"),
w = love.graphics.newImage("images/player/playerw.png"),
}
}
function pl.update() --Movement and stuff
if love.keyboard.isDown("w") then
pl.y = pl.y - pl.spd
pl.dir = "n"
elseif love.keyboard.isDown("a") then
pl.x = pl.x - pl.spd
pl.dir = "w"
elseif love.keyboard.isDown("s") then
pl.y = pl.y + pl.spd
pl.dir = "s"
elseif love.keyboard.isDown("d") then
pl.x = pl.x + pl.spd
pl.dir = "e"
end
end
function pl.draw() --draws the player. determines graphic to load and draws it.
if dir == "n" then
love.graphics.draw(pl.img.n)
elseif dir == "s" then
love.graphics.draw(pl.img.s)
elseif dir == "e" then
love.graphics.draw(pl.img.e)
elseif dir == "w" then
love.graphics.draw(pl.img.e)
end
end
function love.load()
require("player")
love.graphics.setDefaultFilter("nearest")
end
function love.update(dt)
pl.update()
end
function love.draw()
pl.draw()
love.graphics.print(pl.dir,0,0)
love.graphics.rectangle("fill", pl.x, pl.y, 32, 32)
end
I expect the images (playern, players, etc.) to appear but they do not show up. No error messages appear when running. I don't know if its the player or main.
解决方案
问题是pl.draw()
您使用的是dir
, 而不是pl.dir
. 此外,如果您希望播放器图像随播放器移动,则需要添加x
和y
变量作为参数。请参见下面的示例:
if pl.dir == "n" then
love.graphics.draw(pl.img.n, pl.x, pl.y)
推荐阅读
- ios - Cocoapod 系统设置问题 - 'pod install' 出错
- c++ - CMake 无法在 Clion 中链接 Boost 正则表达式库
- css - Select2 - 我们可以设置下拉菜单以匹配页面其余部分的样式吗?
- arrays - 如何控制在 Google 表格中触发多少个单元格
- ios - 未调用委托函数
- javascript - 使用javascript单击突出显示的元素
- reactjs - 我无法在 Azure Linux 环境中仅部署 ReactJS 应用程序的构建文件夹
- python - Pandas Out of Bounds 时间戳解决方法
- python - Python - 重复任务
- rust - 重新借用嵌套的可变引用