local player
local p_x
local p_y
local p_speed
local stillhere
function love.draw()
love.graphics.setBackgroundColor(0,0,0)
end
function love.load()
player = love.graphics.newImage('human1.png')
p_x = 0
p_y = 0
p_speed = 100
end
function love.update(dt)
--Left/Right
if love.keyboard.isDown("a") then
p_x = p_x - p_speed * dt
player = love.graphics.newImage('human3.png')
end
if love.keyboard.isDown("d") then
p_x = p_x + p_speed * dt
player = love.graphics.newImage('human1.png')
end
--Up/Down
if love.keyboard.isDown("s") then
p_y = p_y + p_speed * dt
player = love.graphics.newImage('human4.png')
end
if love.keyboard.isDown("w") then
p_y = p_y - p_speed * dt
player = love.graphics.newImage('human2.png')
end
-- Left/Right Collision
if p_x <0 then
p_x = 0
end
if p_x > love.graphics.getWidth() - player:getWidth() then
p_x = love.graphics.getWidth() - player:getWidth()
end
--Up/Down Collision
if p_y < 0 then
p_y = 0
end
if p_y > love.graphics.getHeight() - player:getHeight() then
p_y = love.graphics.getHeight() - player:getHeight()
end
end
function love.draw()
love.graphics.draw(player, p_x, p_y)
end
love.timer.sleep(5)
function love.load()
stillhere = love.graphics.newImage("waysh.png")
stillhere_x = 100
stillhere_y = 100
end
So what did I do wrong, and how do I read those messages so i don't need help anymore?
I can't help you with the code itself right now, but an error message basically tells you the file (main.lua) and the line (43) in which the error occurs. The traceback shows you the "stack", the sequence of functions before the error.
As I said, in this case the error is in the 43rd line in your main.lua.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Nixola wrote:I can't help you with the code itself right now, but an error message basically tells you the file (main.lua) and the line (43) in which the error occurs. The traceback shows you the "stack", the sequence of functions before the error.
As I said, in this case the error is in the 43rd line in your main.lua.
But I don't know why line 43 was wrong, I removed something about text and then it was fine.
It means that one of the values on line 43 is nil. That value would be "p_x", as the other value on line 43 is clearly a number.
The problem is that you define love.load twice, so the second one overwrites the first; preventing p_x from being set initially.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Also, aside from that issue... Instead of calling 'newImage' every time you change directions, you could make a table of the sprites and have a variable for which to use:
local img = {
idle = love.graphics.newImage('playerIdle.png'),
up = love.graphics.newImage('playerUp.png'),
down = love.graphics.newImage('playerDown.png'),
left = love.graphics.newImage('playerLeft.png'),
right = love.graphics.newImage('playerRight.png')
}
player.img = 'idle'
-- In love.update
if love.keyboard.isDown('w') then player.img = 'up'
elseif love.keyboard.isDown('s') then player.img = 'down'
elseif love.keyboard.isDown('a') then player.img = 'left'
elseif love.keyboard.isDown('d') then player.img = 'right'
else player.img = 'idle'
end
-- Then in love.draw
love.graphics.draw(img[player.img], player.x, player.y)