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

时间:2019-07-11 18:48:00

标签: lua drawing love2d

本应显示的图像未显示。我将播放器代码和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.

1 个答案:

答案 0 :(得分:1)

问题在于,在pl.draw()中,您使用的是dir,而不是pl.dir。另外,如果您希望播放器图像随播放器一起移动,则需要添加xy变量作为参数。请参见下面的示例:

if pl.dir == "n" then
    love.graphics.draw(pl.img.n, pl.x, pl.y)