Use multiple keys-[Movement]

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
SirFotherington
Prole
Posts: 11
Joined: Sat Feb 23, 2013 11:29 pm

Use multiple keys-[Movement]

Post by SirFotherington »

I was wondering how I would go about making my character run in a game? So far I have assigned w a s and d to the movements and I want to use LShift and those keys to make him move faster. I made an attempt but it did not work. Help is appreciated =)

Code: Select all

function love.update()
	if love.keyboard.isDown("d") then 
		joex = joex + 5
	end
	if love.keyboard.isDown("a") then
		joex = joex - 5
	end
	if love.keyboard.isDown("s") then
		joey = joey + 5
	end
	if love.keyboard.isDown("w") then
		joey= joey -5
	end
	if love.keyboard.isDown("lshift""d") then 
		joex = joex + 12
	end
	if love.keyboard.isDown("lshift""a") then
		joex = joex - 12
	end
	if love.keyboard.isDown("lshift""s") then
		joey = joey + 12
	end
	if love.keyboard.isDown("lshift""w") then
		joey= joey -12
	end
	
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Use multiple keys-[Movement]

Post by Nixola »

Code: Select all

function love.update(dt)
   if love.keyboard.isDown("lshift", "rshift") then --this is true when at least one of those keys is held down
      local speed = 12*60
   else
      local speed = 5*60
   end
   if love.keyboard.isDown("d") then 
      joex = joex + speed
   end
   if love.keyboard.isDown("a") then
      joex = joex - speed
   end
   if love.keyboard.isDown("s") then
      joey = joey + speed
   end
   if love.keyboard.isDown("w") then
      joey= joey - speed
   end
end
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
SirFotherington
Prole
Posts: 11
Joined: Sat Feb 23, 2013 11:29 pm

Re: Use multiple keys-[Movement]

Post by SirFotherington »

Awesome thanks im putting this in but still nothing happens. I may have put it in wrong. If you get time here is now the code:

Code: Select all

function love.load()

	medium = love.graphics.newFont(45)

	joe = love.graphics.newImage("pic/joe.png")

	joex = 200
	joey = 200
end


	function love.update(dt)
   if love.keyboard.isDown("lshift", "rshift") then --this is true when at least one of those keys is held down
      local speed = 12*60
   else
      local speed = 5*60
   end
   if love.keyboard.isDown("d") then
      joex = joex + 4
   end
   if love.keyboard.isDown("a") then
      joex = joex - 4
   end
   if love.keyboard.isDown("s") then
      joey = joey + 4
   end
   if love.keyboard.isDown("w") then
      joey= joey - 4
   end
   
   end
   

function love.draw()
	love.graphics.setColor(255,255,255)
	love.graphics.draw(joe,joex,joey)
	

end

User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Use multiple keys-[Movement]

Post by Nixola »

You should add/subtract speed instead of 4
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
SirFotherington
Prole
Posts: 11
Joined: Sat Feb 23, 2013 11:29 pm

Re: Use multiple keys-[Movement]

Post by SirFotherington »

Also what does this small segment mean?

Code: Select all

local speed = 12*60
   else
      local speed = 5*60
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Use multiple keys-[Movement]

Post by Nixola »

It makes the variable speed equal to 12 times 60 if either left shift or right shift is held down, or equal to 5 times 60 otherwise; also, when you add/subtract the speed to Joe's coordinates you should multiply it by dt first, so that his speed isn't framerate dependant. Oh, and you may want to take a look at tables
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
SirFotherington
Prole
Posts: 11
Joined: Sat Feb 23, 2013 11:29 pm

Re: Use multiple keys-[Movement]

Post by SirFotherington »

Oh thanks! And yeah im literally just watching one right now!
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Use multiple keys-[Movement]

Post by Robin »

The local should be outside of the if:

Code: Select all

function love.update(dt)
   local speed
   if love.keyboard.isDown("lshift", "rshift") then --this is true when at least one of those keys is held down
      speed = 12*60
   else
      speed = 5*60
   end
   if love.keyboard.isDown("d") then 
      joex = joex + speed
   end
   if love.keyboard.isDown("a") then
      joex = joex - speed
   end
   if love.keyboard.isDown("s") then
      joey = joey + speed
   end
   if love.keyboard.isDown("w") then
      joey= joey - speed
   end
end
Also, really consider using dt, otherwise the game will be really slow (as in "the player sprite looks like it's running, but it feels like it's crawling") or really fast (as in "the enemies keep killing me before I have a chance to see them" fast), depending on how fast your computer is.
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot] and 4 guests