Hi,
I've just started learning to create 2D games with LOVE, and I'm kind of stuck with this one little problem.
If you run this and press directional keys, the cursor moves ridiculously fast.
It's because I'm moving the unit 35 pixels at a time, but I would like it to move much slowly.
In other words, I would like the system to recognize key input less often, if that's at all possible.
Any suggestions? Please help >_<;
Here's the code for your convenience:
(you can simple copy & paste the whole thing and it will run fine)
--main.lua
-- LOAD --
function love.load()
img = {
unit1 = love.graphics.newImage("unit1.png"),
}
units = {
{ -- cursor --
pos = { 0 *35+1, 14 *35+1 }, -- coordinates
--speed = 0.1, -- movement speed
moveDir = { 0, 0 }, -- movement direction
},
{ -- Unit #1 --
pos = { 7 *35+1, 8 *35+1 }, -- coordinates
speed = 70, -- movement speed
moveDir = { 0, 0 }, -- movement direction
},
{ -- Unit #2 --
pos = { 10 *35+1, 2 *35+1 }, -- coordinates
speed = 70, -- movement speed
moveDir = { 0, 0 }, -- movement direction
},
}
love.graphics.setBackgroundColor(200,230,230) -- BG color
end
-- UPDATE --
function love.update(dt)
units[1].pos = {
units[1].pos[1] + (units[1].moveDir[1] * 35),
units[1].pos[2] + (units[1].moveDir[2] * 35)
}
end
-- DRAW --
function love.draw(dt)
for i=35, 15 *35, 35 do
--local tmp = love.graphics.setColor(0,0,0)
love.graphics.line(i, 0, i, 15 *35)
love.graphics.line(0, i, 15 *35, i)
end
love.graphics.draw(img.unit1, units[1].pos[1], units[1].pos[2])
love.graphics.draw(img.unit1, units[2].pos[1], units[2].pos[2])
love.graphics.draw(img.unit1, units[3].pos[1], units[3].pos[2])
end
-- KEY PRESSED / RELEASED --
function love.keypressed(key, isrepeat)
if key == "up" then
units[1].moveDir[2] = -1
elseif key == "down" then
units[1].moveDir[2] = 1
elseif key == "right" then
units[1].moveDir[1] = 1
elseif key == "left" then
units[1].moveDir[1] = -1
elseif key == "escape" then
love.event.quit()
end
end
function love.keyreleased(key, isrepeat)
if key == "up" then
units[1].moveDir[2] = 0
elseif key == "down" then
units[1].moveDir[2] = 0
elseif key == "right" then
units[1].moveDir[1] = 0
elseif key == "left" then
units[1].moveDir[1] = 0
end
end
Question (urgent!) - a simple 2D game
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- DaedalusYoung
- Party member
- Posts: 413
- Joined: Sun Jul 14, 2013 8:04 pm
Re: Question (urgent!) - a simple 2D game
Use dt.
Code: Select all
function love.update(dt)
units[1].pos = {
units[1].pos[1] + (units[1].moveDir[1] * (35 * dt)),
units[1].pos[2] + (units[1].moveDir[2] * (35 * dt))
}
end
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 2 guests