图像不会使用多个文件绘制在屏幕上
Image will not draw on screen using multiple files
应该显示的图像没有显示。我将播放器代码和 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)
应该显示的图像没有显示。我将播放器代码和 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)