Page 1 of 1
Use multiple keys-[Movement]
Posted: Sun Feb 24, 2013 11:31 am
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
Re: Use multiple keys-[Movement]
Posted: Sun Feb 24, 2013 11:39 am
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
Re: Use multiple keys-[Movement]
Posted: Sun Feb 24, 2013 11:46 am
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
Re: Use multiple keys-[Movement]
Posted: Sun Feb 24, 2013 11:48 am
by Nixola
You should add/subtract speed instead of 4
Re: Use multiple keys-[Movement]
Posted: Sun Feb 24, 2013 11:49 am
by SirFotherington
Also what does this small segment mean?
Code: Select all
local speed = 12*60
else
local speed = 5*60
Re: Use multiple keys-[Movement]
Posted: Sun Feb 24, 2013 11:54 am
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
Re: Use multiple keys-[Movement]
Posted: Sun Feb 24, 2013 11:55 am
by SirFotherington
Oh thanks! And yeah im literally just watching one right now!
Re: Use multiple keys-[Movement]
Posted: Sun Feb 24, 2013 4:11 pm
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.