Heatso
Heatso

Reputation: 77

Image will not draw on screen using multiple files

The image that is supposed to display is not displaying. I am putting the player code and main.lua in separate files. It is my first time using multiple files in any programing language

I have tried to create a rectangle to make sure it's existing. The rectangle does show up, but the image does not.

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.

Upvotes: 1

Views: 167

Answers (1)

SkippyNBS
SkippyNBS

Reputation: 777

The issue is that in pl.draw() you are using dir, instead of pl.dir. Additionally if you want the player image to move with the player you need to add the x and y variables as parameters. See the example below:

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

Upvotes: 1

Related Questions