How to move an image a certain way?

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
Nero-One
Prole
Posts: 2
Joined: Fri Feb 15, 2013 5:12 am

How to move an image a certain way?

Post by Nero-One »

Currently in my code, there is a player object that moves smoothly, my only problem is that I can't get it to move forwards directly as in I can scroll side to side with 'A' and 'D' but not directly forward with 'W' and 'S'. Button combos such as 'W+A' and 'A+S' work though...Please help out!!

Code: Select all

function love.load()	
	love.graphics.setBackgroundColor(100, 149, 237)
	x = 50
	y = 50
	speed = 100
	playerIdle = love.graphics.newImage("gfx/Person/0.png")
	playerLeft  = love.graphics.newImage("gfx/Person/0.png")
	playerRight = love.graphics.newImage("gfx/Person/0.png")
	playerDown  = love.graphics.newImage("gfx/Person/0.png")
	playerUp= love.graphics.newImage("gfx/Person/0.png")
	
	player = playerIdle --no movement

end

function love.update(dt)
	if love.keyboard.isDown("d") then
		x = x + (speed * dt)
		player = playerRight
	elseif love.keyboard.isDown("a") then
		x = x - (speed * dt)
		player = playerLeft
	if love.keyboard.isDown("s") then
		y = y + (speed * dt)
		player = playerDown
    elseif love.keyboard.isDown("w") then
        y = y - (speed * dt)
		player = playerUp
		end
	end
end

function love.draw()
	love.graphics.draw(player, x, y)
	love.graphics.draw(playerLeft, x, y, math.rad(45), -1, 1, width, 0)
end
scutheotaku
Party member
Posts: 235
Joined: Sat Dec 15, 2012 6:54 am

Re: How to move an image a certain way?

Post by scutheotaku »

You end the "elseif love.keyboard.isDown("a")" statement after the up and down movement part, instead of right after that statement. That's why left-diagonal movement works, because as your code is written it will only check for vertical movement if "a" is held down.

Try this:

Code: Select all

function love.update(dt)
   if love.keyboard.isDown("d") then
      x = x + (speed * dt)
      player = playerRight
   elseif love.keyboard.isDown("a") then
      x = x - (speed * dt)
      player = playerLeft
   --right here :)
   end
   --^
   if love.keyboard.isDown("s") then
      y = y + (speed * dt)
      player = playerDown
    elseif love.keyboard.isDown("w") then
        y = y - (speed * dt)
      player = playerUp
   end
end
That should work :)
Post Reply

Who is online

Users browsing this forum: Google [Bot], MummyTheTiger and 2 guests