使用多个文件时无法在屏幕上绘制图像

应该显示的图像没有出现。我将 player 代码和 main.lua 放在不同的文件中。这是我第一次在任何编程语言中使用多个文件

我试图创建一个矩形来确保它存在。矩形确实显示出来了,但图像没有。

this is player.lua
    pl = {   -- pl 代表玩家。
    x = 100, -- 玩家的 x 坐标
    y = 100, -- 玩家的 y 坐标
    spd = 4, -- 玩家的速度
    dir = "", -- 玩家的方向 (n=北 s=南 e=东 w=西
    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() -- 移动和其他操作
    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() -- 画出玩家,确定要加载的图形并将其画出。
 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

我期望图像 (playern,players 等) 出现,但它们没有显示出来。运行时没有错误消息。我不知道是 player 还是 main 的问题。
点赞
用户3161725
用户3161725

问题在于 pl.draw() 中你使用了 dir 而不是 pl.dir。此外,如果你想让玩家图像随着玩家移动,你需要将 xy 变量作为参数添加进去。请参考下面的示例:

if pl.dir == "n" then
    love.graphics.draw(pl.img.n, pl.x, pl.y)
2019-07-11 23:38:46