Page 2 of 2

Re: Newbie has a few basic questions

Posted: Wed Mar 02, 2011 5:59 pm
by tentus
obur wrote:i löve you! i really löve you! :nyu:

oh, but there's a problem. i'm making 2 different if statements for walking in 2 directions at the same time. like going up and right at the same time. however, if i make 2 different if statements in here, one of them will drop to else all the time.

Code: Select all

if (up)
 go up;
else if (down)
 go down;

if (right)
 go right;
else if (left)
 go left;
now, if i make only 1 if statement, i can't go in 2 directions at the same time.

by the way, do you know why my previous idea didn't work? i mean that sentinel stuff. thanks again :)
Use something like this:

Code: Select all

function love.load()
	player = {
		x = 100,
		y = 100,
		speed = 100
	}
end

function love.update(dt)
	if love.keyboard.isDown(key.up) and love.keyboard.isDown(key.down) then
		-- canceled out
	elseif love.keyboard.isDown(key.up) then
		move(0, -dt)
	elseif love.keyboard.isDown(key.down) then
		move(0, dt)
	end
	if love.keyboard.isDown(key.left) and love.keyboard.isDown(key.right) then
		-- canceled out
	elseif love.keyboard.isDown(key.left) then
		move(-dt, 0)
	elseif love.keyboard.isDown(key.right) then
		move(dt, 0)
	end
end

function move(x, y)
	player.x = player.x + (x * player.speed)
	player.y = player.y + (y * player.speed)
end
This will move the player up or down and left or right at whatever speed the player has set (a speed of 1 would move you one pixel per second, a speed of 100 would move you 100 pixels per sec, etc). If you hold up both the up key and down key, nothing will happen. Same for left and right.

Really, I think you need to focus on learning Lua a bit. Download https://github.com/vrld/love-console and play around with it, get the hang of the language.