キーボード入力を使用して画面上のラベルを翻訳しようとしています。現在、下と左のみが機能しています。私のコードは以下の通りです。Love2dが画面上のオブジェクトを移動する
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文の順番ということを示しているが、私はすべての4つを同時に動作させることはできません。このようなものに
各値を両方の可能性と照合し、それぞれの値をそれぞれの値に設定します。あなたの頭の中の 'love.update'のロジックにしたがって、任意のキー入力に対して' dy'と 'dx'を追跡します。 (1つの値を変更するだけで、すべての変数には値があることを覚えておいてください) –