Page 1 of 1

simple right/left movement doesnt work

Posted: Fri Apr 04, 2014 4:12 pm
by ottohero
i am just learning how to do coding in love2d and lua, but when i try to make this super simple up/down/left/right movement it goes diagonally, for some reason... here's the problem code(in player.lua):

Code: Select all

player = {}
player.x = 300
player.y = 300
player.speed = 10
player.health = 20
player.damage = 2


player.pic = love.graphics.newImage("anka.png")

function playerDraw()
	love.graphics.setColor(255, 255, 255)
	love.graphics.draw(player.pic, player.x, player.x)
end
function playerMove()
	if love.keyboard.isDown("up") then
		player.y = player.y - player.speed
	end
	if love.keyboard.isDown("down") then
		player.y = player.y + player.speed
	end
	if love.keyboard.isDown("left") then
		player.x = player.x - player.speed
	end
	if love.keyboard.isDown("right") then
		player.x = player.x + player.speed
	end
end
to clarify myself, i did put playerMove() and playerDraw() in the love.update() and love.draw() functions in main.lua.

Re: simple right/left movement doesnt work

Posted: Fri Apr 04, 2014 5:34 pm
by WetDesertRock
That looks like it should work. The main problem I see here is the fact that you aren't taking into account the timedelta between frames. What this means is that your player will move faster or slower depending on how many frames per second you get. To fix this use the dt (delta time) value of the love.update(dt) function. Once you got dt into your playerMove() (which will now look like playerMove(dt)), you can multiply the speed by the dt to fix this.

Re: simple right/left movement doesnt work

Posted: Fri Apr 04, 2014 7:04 pm
by Ragzouken

Code: Select all

love.graphics.draw(player.pic, player.x, player.x)
you're drawing at x,x instead of x,y

Re: simple right/left movement doesnt work

Posted: Sat Apr 05, 2014 5:42 pm
by ottohero
Ragzouken wrote:

Code: Select all

love.graphics.draw(player.pic, player.x, player.x)
you're drawing at x,x instead of x,y
oooooh!!! :rofl: thak you so much!