我对Lua和Love2D完全不熟悉,可能根本不理解这些概念。 这是一个Love2D教程,我想改变它,所以当我按下“a”时,例如,在键盘上,对象将交换(从仓鼠到汽车)等等。
你可以帮帮我吗?-- Tutorial 1: Hamster Ball
-- Add an image to the game and move it around using
-- the arrow keys.
-- compatible with löve 0.6.0 and up
function love.load()
hamster = love.graphics.newImage("hamster.png")
auto = love.graphics.newImage("auto.png")
x = 50
y = 50
speed = 300
end
function love.update(dt)
if love.keyboard.isDown("right") then
x = x + (speed * dt)
end
if love.keyboard.isDown("left") then
x = x - (speed * dt)
end
if love.keyboard.isDown("down") then
y = y + (speed * dt)
end
if love.keyboard.isDown("up") then
y = y - (speed * dt)
end
if love.keyboard.isDown("escape") then
love.event.quit()
end
if love.keyboard.isDown("a") then
love.draw(auto,x,y)
end
end
function love.draw()
love.graphics.draw(hamster, x, y)
end
答案 0 :(得分:0)
我建议使用love.update
来更新状态。不要画画。然后在love.draw
中完成所有绘图。解决方案可能是:
local state = {}
function love.load()
hamster = love.graphics.newImage("hamster.png")
auto = love.graphics.newImage("auto.png")
state.activeImage = hamster
state.activeImageName = "hamster"
-- <snip> ...
end
function love.update(dt)
-- <snip> ...
if love.keyboard.isDown("a") then
if state.activeImageName == "hamster" then
state.activeImage = auto
state.activeImageName = "auto"
else
state.activeImage = hamster
state.activeImageName = "hamster"
end
end
end
function love.draw()
love.graphics.draw(state.activeImage, x, y)
end
答案 1 :(得分:0)
非常感谢Corbin,我认为没有“州”局部变量。你的解决方案对我很有启发。现在它正在发挥作用。
-- Tutorial 1: Hamster Ball
-- Add an image to the game and move it around using
-- the arrow keys.
-- compatible with löve 0.6.0 and up
function love.load()
hamster = love.graphics.newImage("hamster.png")
auto = love.graphics.newImage("auto.png")
activeImage = hamster
activeImageName = "hamster"
x = 50
y = 50
speed = 300
end
function love.update(dt)
if love.keyboard.isDown("right") then
x = x + (speed * dt)
end
if love.keyboard.isDown("left") then
x = x - (speed * dt)
end
if love.keyboard.isDown("down") then
y = y + (speed * dt)
end
if love.keyboard.isDown("up") then
y = y - (speed * dt)
end
if love.keyboard.isDown("escape") then
love.event.quit()
end
if love.keyboard.isDown("a") then
activeImage = auto
activeImageName = "auto"
end
if love.keyboard.isDown("h") then
activeImage = hamster
activeImageName = "hamster"
end
end
function love.draw()
love.graphics.draw(activeImage, x, y)
end