【发布时间】:2016-05-06 13:28:41
【问题描述】:
我正在尝试使用键盘输入来翻译屏幕周围的标签。目前只有向下和向左起作用。我的代码如下。
debug = true
down = 0
up = 0
left = 0
right = 0
text = 'non'
x = 100
y = 100
dx = 0
dy = 0
function love.load(arg)
end
function love.update(dt)
if love.keyboard.isDown('escape') then
love.event.push('quit')
end
if up == 1 then
dy = -1
end
if up == 0 then
dy = 0
end
if down == 1 then
dy = 1
end
if down == 0 then
dy = 0
end
if right == 1 then
dx = 1
end
if right == 0 then
dx = 0
end
if left == 1 then
dx = -1
end
if left == 0 then
dx = 0
end
end
function love.keypressed(key)
if key == 'up' or key == 'w' then
text = 'up'
up = 1
end
if key == 'down' or key == 's' then
text = 'down'
down = 1
end
if key == 'right' or key == 'd' then
text = 'right'
right = 1
end
if key == 'left' or key == 'a' then
text = 'left'
left = 1
end
end
function love.keyreleased(key)
text = 'non'
if key == 'up' or key == 'w' then
up = 0
end
if key == 'down' or key == 's' then
down = 0
end
if key == 'right' or key == 'd' then
right = 0
end
if key == 'left' or key == 'a' then
left = 0
end
end
function love.draw(dt)
x = x + dx
y = y + dy
love.graphics.print(text, x, y)
end
实验表明,love.update(dt) 部分中 if 语句的顺序会影响哪些方向有效,但我无法让所有四个同时工作。
【问题讨论】:
-
您根据两种可能性检查每个值,然后为每个值设置一个共享值。遵循
love.update中的逻辑,在您的脑海中跟踪任何给定按键的dy和dx。 (请记住,您可能只更改一个值,但所有变量都有值。)