Page 1 of 1
Drawing order not working properly, could use some help!
Posted: Sun Feb 25, 2018 11:42 am
by SwoffleTheGreat
Hello, you fine ladies and gentlemen! I'll get straight to the point - I'm a newbie when it comes to LOVE 2D, and yes, I've searched pretty much the entire internet for a solution - so It'd be dandy if I got my solution here. Basically I'm trying to make a game with hand drawn backgrounds, this means that I've tried using drawings orders to give the illusion of depth. This worked rather fine except for the part regarding having the player sprite move. It'd be nice of you lot if you could tell me what's wrong with this code (see files).
Thanks in regards!
Re: Drawing order not working properly, could use some help!
Posted: Sun Feb 25, 2018 12:33 pm
by PGUp
Alright, this is weird.. why don't you just use 1 love.draw(), love.update() and love.load() function ? Or if you don't want it to be a mess do things in separated files. For your player movement problem, if you do things like this
Code: Select all
function love.update()
if love.keyboard.isDown("right") then
player.x = player.x + 1
if love.keyboard.isDown("left") then
player.x = player.x - 1
end
end
end
to make the player move left.. you have to press the right button first, then the left, this is absolutely not how Lua works.. it should be like this
Code: Select all
function love.update()
if love.keyboard.isDown("right") then
player.x = player.x + 1
end
if love.keyboard.isDown("left") then
player.x = player.x - 1
end
end
Re: Drawing order not working properly, could use some help!
Posted: Sun Feb 25, 2018 12:42 pm
by pgimeno
Could you please paste the file in text form, or in .love form?
From a quick glance, you're making something like this (schematically):
Code: Select all
function a()
function b()
function c()
end
end
end
That means that c can't be called unless b has been called, and b can't be called unless a has been called. I guess that's not what you intend. The 'end' keyword that closes the function normally appears before the next 'function' keyword.
Similarly, you're doing something like this:
Code: Select all
if condition 1 then
do stuff 1
if condition 2 then
do stuff 2
if condition 3 then
do stuff 3
end
end
end
Contrary to what you may think that does, the test for condition 2 only executes when condition 1 is already true, and the test for condition 3 only executes when both condition 1 and condition 2 are true. Proper indentation of the code will show this:
Code: Select all
if condition 1 then
do stuff 1
if condition 2 then
do stuff 2
if condition 3 then
do stuff 3
end
end
end
The correct way to write tests that must all be executed without depending on the former is this one:
Code: Select all
if condition1 then
do stuff 1
end
if condition2 then
do stuff 2
end
if condition 3 then
do stuff 3
end
Note that Lua is not like Python. Lua ignores indentation, and the criterion for what is inside what, is based only on keywords, like if... then... end, for... do... end, etc. The 'end' does in Lua what unindenting does in Python.
Re: Drawing order not working properly, could use some help!
Posted: Sun Feb 25, 2018 3:06 pm
by zorg
And as PGUp has said, you absolutely MUST have only one of each löve callback, since otherwise, the ones "lower" in the code, or in other words, defined later in time, will overwrite the ones that were defined before.
If you want to separate out code parts that need to be called in love.load, or want separate "states" with their own logic update functions and draw functions, then there are solutions to that as well, but not you defining love.update or love.draw multiple times.
Re: Drawing order not working properly, could use some help!
Posted: Sun Feb 25, 2018 3:44 pm
by SwoffleTheGreat
Heyo, you lot! Thanks for the informative replies, I very much appreciate it!
I've tried to work a bit with the things you lot said, but my code still won't work. I'm not even getting an error, rather than that - I still simply can't move my character! I posted a lua file of the code - I'd love for you guys to give it a quick look. Thanks in advance!
Re: Drawing order not working properly, could use some help!
Posted: Sun Feb 25, 2018 3:57 pm
by zorg
You could have just put that into a code block; here, i tried to fix most things; haven't tested it, but now it looks way more organized.
Code: Select all
local FirstSong
local ghetto, person
local objects
local plrx, plry
function love.load()
FirstSong = love.audio.newSource("AllStar.mp3")
FirstSong:setVolume(0.2)
FirstSong:setPitch(1)
FirstSong:play()
ghetto = love.graphics.newImage("TestMap.PNG")
person = love.graphics.newImage("Confederate.PNG")
-- No need for sorting, since this already is sorted according to the second value.
objects = {}
objects[1] = {550,370}
objects[2] = {220,390}
plrx = 200 --love.graphics.getWidth() / 2
plry = 200 --love.graphics.getHeight() / 2
speed = 300
end
function love.update(dt)
if love.keyboard.isDown("right") then
plrx = plrx + (speed * dt)
elseif love.keyboard.isDown("left") then
plrx = plrx - (speed * dt)
end
if love.keyboard.isDown("down") then
plry = plry + (speed * dt)
elseif love.keyboard.isDown("up") then
plry = yplry - (speed * dt)
end
end
function love.draw()
-- You need to draw an image giving the top left to the function.
love.graphics.draw(ghetto, 0, 0)
--for i,v in ipairs(objects) do
-- nothing here...?
--end
--if not drawn then -- if the person is below all objects it won't be drawn within the for loop <- that's not how things work.
-- love.graphics.draw(person, character[1] - person:getWidth()/2, character[2] - person:getHeight())
--end
love.graphics.draw(person, plrx, plry, dt)
end
Re: Drawing order not working properly, could use some help!
Posted: Sun Feb 25, 2018 4:11 pm
by SwoffleTheGreat
My comrades! It worked! Thanks alot for the help, you lot! Incase anyone else comes across the same problem - Zorg fixed my script in a truly excellent manner. Thanks a bunch, you lot!