simple right/left movement doesnt work

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
ottohero
Prole
Posts: 2
Joined: Wed Jan 29, 2014 6:53 pm

simple right/left movement doesnt work

Post 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.
WetDesertRock
Citizen
Posts: 67
Joined: Fri Mar 07, 2014 8:16 pm

Re: simple right/left movement doesnt work

Post 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.
User avatar
Ragzouken
Citizen
Posts: 84
Joined: Fri Aug 10, 2012 7:59 am
Contact:

Re: simple right/left movement doesnt work

Post by Ragzouken »

Code: Select all

love.graphics.draw(player.pic, player.x, player.x)
you're drawing at x,x instead of x,y
ottohero
Prole
Posts: 2
Joined: Wed Jan 29, 2014 6:53 pm

Re: simple right/left movement doesnt work

Post 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!
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Bing [Bot], Google [Bot], Semrush [Bot] and 5 guests