Love2D,LUA 补间重置
Love2D, LUA Tween resetting
我的补间有问题。当按住该按钮时,我正在使用 tween.lua 向左或向右移动我的角色。释放玩家 returns 回到中间。当角色向左或向右移动时,补间效果非常好,但是由于某种原因,当它必须回到中间时,角色只是扭曲到那里而不是补间。我怀疑是基础 X 覆盖了它,还是我没有在正确的时间重新设置。
这是我的代码:
--Local variables
local lg = love.graphics
local lk = love.keyboard
function player:load(arg) --Player load function. Called when loaded.
self.img = lg.newImage(currentPimg)
playerWidth = player.img:getWidth() --Gets player image width and sets as a variable
self.mid = width/2 - playerWidth/2
self.left = 100 - playerWidth/2
self.right = width - 100 - playerWidth/2
self.x = player.mid
self.y = height-150
self.speed = 0.04
GoMid = tween.new(player.speed , player, {x=player.mid}, 'linear')
GoLeft = tween.new(player.speed , player, {x=player.left}, 'linear')
GoRight = tween.new(player.speed , player, {x=player.right}, 'linear')
end
function player:update(dt) --Player update function. Called each frame, passes DT (delta time)
playerWidth = player.img:getWidth() --Gets player image width and sets as a variable
if LeftStarted and not isLeft then
GoLeft:reset()
LeftStarted = false
end
if RightStarted and not isRight then
GoRight:reset()
RightStarted = false
end
if MidStarted and not isMid then
GoMid:reset()
MidStarted = false
end
if isMid then --If is true then do action
GoMid:update(dt)
MidStarted = true
end
if isRight then --If is true then do action
GoRight:update(dt)
RightStarted = true
end
if isLeft then --If is true then do action
GoLeft:update(dt)
LeftStarted = true
end
if lk.isDown("left", "a") and not isRight then --this check needs to be done since the code is executed the first time. If I do not check weird stuff happens
isLeft = true
isRight, isMid = false
elseif lk.isDown("right", "d") then --checks if the button is down and returns true if it is
isRight = true
isLeft, isMid = false
else -- if nothing is down resets player to mid and all variables to false
isLeft, isRight = false
isMid = true
end
end
function player:draw(dt) --Draw function. Called each frame, passes DT (delta time)
lg.draw(player.img, player.x, player.y) --Draws player image at X and Y
end
您的代码的工作版本
所以,在对代码进行了大量的修改之后,我可以向您展示以下内容:
player = { } --Required table thing
--Local variables
local lg = love.graphics
local lk = love.keyboard
function player:load(arg) --Player load function. Called when loaded.
self.img = lg.newImage(currentPimg)
playerWidth = player.img:getWidth() --Gets player image width and sets as a variable
self.mid = width/2 - playerWidth/2
self.left = 100 - playerWidth/2
self.right = width - 100 - playerWidth/2
self.x = player.mid
self.y = height-150
self.speed = 0.5
GoMid = tween.new(player.speed , player, {x=player.mid}, 'linear')
GoLeft = tween.new(player.speed , player, {x=player.left}, 'linear')
GoRight = tween.new(player.speed , player, {x=player.right}, 'linear')
end
function player:update(dt) --Player update function. Called each frame, passes DT (delta time)
playerWidth = player.img:getWidth() --Gets player image width and sets as a variable
if LeftStarted and not isLeft then
GoMid = tween.new(player.speed , player, {x=player.mid}, 'linear')
LeftNeedsReset = true
LeftStarted = false
end
if RightStarted and not isRight then
GoMid = tween.new(player.speed , player, {x=player.mid}, 'linear')
RightNeedsReset = true
RightStarted = false
end
if isMid then --If is true then do action
GoMid:update(dt)
end
if isRight then --If is true then do action
if RightNeedsReset then
GoRight:reset()
RightNeedsReset = false
end
GoRight:update(dt)
RightStarted = true
end
if isLeft then --If is true then do action
if LeftNeedsReset then
GoLeft:reset()
LeftNeedsReset = false
end
GoLeft:update(dt)
LeftStarted = true
end
if lk.isDown("left", "a") and not isRight then --this check needs to be done since the code is executed the first time. If I do not check weird stuff happens
isLeft = true
isRight, isMid = false, false
elseif lk.isDown("right", "d") then --checks if the button is down and returns true if it is
isRight = true
isLeft, isMid = false, false
else -- if nothing is down resets player to mid and all variables to false
isLeft, isRight = false, false
isMid = true
end
end
function player:draw(dt) --Draw function. Called each frame, passes DT (delta time)
lg.draw(player.img, player.x, player.y) --Draws player image at X and Y
end
这实现了我认为你想要的(玩家平稳地回到中心),但我仍然认为这不是一个好的解决方案。
我所做的是,每当玩家需要开始向中心移动时,我都会以适当的起点进行新动作,并且我会延迟定向动作的重置,直到有必要,因为它会让玩家跳回起点.
观察
我在使用您的代码时注意到了一些事情。
第一个是您尝试为多个变量赋值。据我所知,它在 Lua 中不是这样工作的。为此,您必须这样写:
isLeft, isRight = false, false
而不是这个:
isLeft, isRight = false
我在调试时花了一段时间才注意到的另一件事是,您以与更新部分不同的顺序编写了说明的重置部分。我不认为这是一个好习惯,除非你有非常充分的理由这样做。
建议
我认为这个库不太适合这个任务(虽然我不是很了解,这是我第一次在调试你的代码时使用它)。这可以通过语言和框架本身的内置功能轻松完成。你可以有一个变量,keeps 跟踪当前位置,然后不断地向按下按钮的方向改变它直到一个限制,然后在释放所有键时将它拖回中心。
我没有测试过这段代码,只是瞎写的,但它可能看起来像这样:
if love.keyboard.isDown("left") then
if currentPosition > middlePosition - movementLimit then
currentPosition = currentPosition - 20 * dt
end
elseif love.keyboard.isDown("right") then
if currentPosition < middlePosition + movementLimit then
currentPosition = currentPosition + 20 * dt
end
else
if currentPosition < middlePosition then
currentPosition = currentPosition + 20 * dt
elseif currentPosition > middlePosition then
currentPosition = currentPosition - 20 * dt
end
end
ps.:我不介意的另一件事是,如果您可以在良好品味的范围内保持源代码中注释的措辞。
[khm.. debugging.lua : 第 7 行.khm..]
希望您的项目取得成功!祝你好运!
我的补间有问题。当按住该按钮时,我正在使用 tween.lua 向左或向右移动我的角色。释放玩家 returns 回到中间。当角色向左或向右移动时,补间效果非常好,但是由于某种原因,当它必须回到中间时,角色只是扭曲到那里而不是补间。我怀疑是基础 X 覆盖了它,还是我没有在正确的时间重新设置。 这是我的代码:
--Local variables
local lg = love.graphics
local lk = love.keyboard
function player:load(arg) --Player load function. Called when loaded.
self.img = lg.newImage(currentPimg)
playerWidth = player.img:getWidth() --Gets player image width and sets as a variable
self.mid = width/2 - playerWidth/2
self.left = 100 - playerWidth/2
self.right = width - 100 - playerWidth/2
self.x = player.mid
self.y = height-150
self.speed = 0.04
GoMid = tween.new(player.speed , player, {x=player.mid}, 'linear')
GoLeft = tween.new(player.speed , player, {x=player.left}, 'linear')
GoRight = tween.new(player.speed , player, {x=player.right}, 'linear')
end
function player:update(dt) --Player update function. Called each frame, passes DT (delta time)
playerWidth = player.img:getWidth() --Gets player image width and sets as a variable
if LeftStarted and not isLeft then
GoLeft:reset()
LeftStarted = false
end
if RightStarted and not isRight then
GoRight:reset()
RightStarted = false
end
if MidStarted and not isMid then
GoMid:reset()
MidStarted = false
end
if isMid then --If is true then do action
GoMid:update(dt)
MidStarted = true
end
if isRight then --If is true then do action
GoRight:update(dt)
RightStarted = true
end
if isLeft then --If is true then do action
GoLeft:update(dt)
LeftStarted = true
end
if lk.isDown("left", "a") and not isRight then --this check needs to be done since the code is executed the first time. If I do not check weird stuff happens
isLeft = true
isRight, isMid = false
elseif lk.isDown("right", "d") then --checks if the button is down and returns true if it is
isRight = true
isLeft, isMid = false
else -- if nothing is down resets player to mid and all variables to false
isLeft, isRight = false
isMid = true
end
end
function player:draw(dt) --Draw function. Called each frame, passes DT (delta time)
lg.draw(player.img, player.x, player.y) --Draws player image at X and Y
end
您的代码的工作版本
所以,在对代码进行了大量的修改之后,我可以向您展示以下内容:
player = { } --Required table thing
--Local variables
local lg = love.graphics
local lk = love.keyboard
function player:load(arg) --Player load function. Called when loaded.
self.img = lg.newImage(currentPimg)
playerWidth = player.img:getWidth() --Gets player image width and sets as a variable
self.mid = width/2 - playerWidth/2
self.left = 100 - playerWidth/2
self.right = width - 100 - playerWidth/2
self.x = player.mid
self.y = height-150
self.speed = 0.5
GoMid = tween.new(player.speed , player, {x=player.mid}, 'linear')
GoLeft = tween.new(player.speed , player, {x=player.left}, 'linear')
GoRight = tween.new(player.speed , player, {x=player.right}, 'linear')
end
function player:update(dt) --Player update function. Called each frame, passes DT (delta time)
playerWidth = player.img:getWidth() --Gets player image width and sets as a variable
if LeftStarted and not isLeft then
GoMid = tween.new(player.speed , player, {x=player.mid}, 'linear')
LeftNeedsReset = true
LeftStarted = false
end
if RightStarted and not isRight then
GoMid = tween.new(player.speed , player, {x=player.mid}, 'linear')
RightNeedsReset = true
RightStarted = false
end
if isMid then --If is true then do action
GoMid:update(dt)
end
if isRight then --If is true then do action
if RightNeedsReset then
GoRight:reset()
RightNeedsReset = false
end
GoRight:update(dt)
RightStarted = true
end
if isLeft then --If is true then do action
if LeftNeedsReset then
GoLeft:reset()
LeftNeedsReset = false
end
GoLeft:update(dt)
LeftStarted = true
end
if lk.isDown("left", "a") and not isRight then --this check needs to be done since the code is executed the first time. If I do not check weird stuff happens
isLeft = true
isRight, isMid = false, false
elseif lk.isDown("right", "d") then --checks if the button is down and returns true if it is
isRight = true
isLeft, isMid = false, false
else -- if nothing is down resets player to mid and all variables to false
isLeft, isRight = false, false
isMid = true
end
end
function player:draw(dt) --Draw function. Called each frame, passes DT (delta time)
lg.draw(player.img, player.x, player.y) --Draws player image at X and Y
end
这实现了我认为你想要的(玩家平稳地回到中心),但我仍然认为这不是一个好的解决方案。
我所做的是,每当玩家需要开始向中心移动时,我都会以适当的起点进行新动作,并且我会延迟定向动作的重置,直到有必要,因为它会让玩家跳回起点.
观察
我在使用您的代码时注意到了一些事情。
第一个是您尝试为多个变量赋值。据我所知,它在 Lua 中不是这样工作的。为此,您必须这样写:
isLeft, isRight = false, false
而不是这个:
isLeft, isRight = false
我在调试时花了一段时间才注意到的另一件事是,您以与更新部分不同的顺序编写了说明的重置部分。我不认为这是一个好习惯,除非你有非常充分的理由这样做。
建议
我认为这个库不太适合这个任务(虽然我不是很了解,这是我第一次在调试你的代码时使用它)。这可以通过语言和框架本身的内置功能轻松完成。你可以有一个变量,keeps 跟踪当前位置,然后不断地向按下按钮的方向改变它直到一个限制,然后在释放所有键时将它拖回中心。
我没有测试过这段代码,只是瞎写的,但它可能看起来像这样:
if love.keyboard.isDown("left") then
if currentPosition > middlePosition - movementLimit then
currentPosition = currentPosition - 20 * dt
end
elseif love.keyboard.isDown("right") then
if currentPosition < middlePosition + movementLimit then
currentPosition = currentPosition + 20 * dt
end
else
if currentPosition < middlePosition then
currentPosition = currentPosition + 20 * dt
elseif currentPosition > middlePosition then
currentPosition = currentPosition - 20 * dt
end
end
ps.:我不介意的另一件事是,如果您可以在良好品味的范围内保持源代码中注释的措辞。 [khm.. debugging.lua : 第 7 行.khm..]
希望您的项目取得成功!祝你好运!