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 =)
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
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
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
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
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.