Love2D,LUA Tween
我的粗花呢有问题。我正在使用tween.lua在按住该按钮时向左或向右移动角色。释放时,玩家返回中间。当角色向左或向右移动时,Tweening非常有效,但是由于某种原因,当角色必须回到中间时,角色只是在那里扭曲,而不是tween。我怀疑要么是基X覆盖了它,要么是我没有在正确的时刻施加压力。 这是我的密码:Love2D,LUA Tween,lua,tween,love2d,Lua,Tween,Love2d,我的粗花呢有问题。我正在使用tween.lua在按住该按钮时向左或向右移动角色。释放时,玩家返回中间。当角色向左或向右移动时,Tweening非常有效,但是由于某种原因,当角色必须回到中间时,角色只是在那里扭曲,而不是tween。我怀疑要么是基X覆盖了它,要么是我没有在正确的时刻施加压力。 这是我的密码: --Local variables local lg = love.graphics local lk = love.keyboard function player:load(arg) -
--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
调试过程中,我花了一段时间才注意到的另一件事是,您以与更新部分不同的顺序编写了方向的重置部分。我不会认为这是一个好习惯,除非你有很强的理由这样做。
建议
在我看来,这个库并不适合这个任务(虽然我不太了解它,但这是我第一次在调试代码时使用它)。这可以通过语言的内置功能和框架本身轻松实现。您可以使用一个变量跟踪当前位置,然后朝着按下按钮的方向不断更改它,直到达到限制,然后在释放所有键时将其拖回中心
我没有测试这段代码,只是盲目编写,但它可能看起来像这样:
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
如果love.keyboard.isDown(“左”)那么
如果当前位置>中间位置-移动限制,则
当前位置=当前位置-20*dt
终止
如果爱。键盘。我就下(“对”)吧
如果当前位置<中间位置+移动限制,则
当前位置=当前位置+20*dt
终止
其他的
如果当前位置<中间位置,则
当前位置=当前位置+20*dt
如果当前位置>中间位置,则
当前位置=当前位置-20*dt
终止
终止
注:另一件我不介意的事情是,如果你能在源代码中保持评论的措辞在良好品味的范围内。 [khm..debug.lua:第7行。khm..]
我希望你的项目成功!祝你好运 我试着调查一下,但我不认为我完全理解你想要实现的目标。你能把整个项目都贴出来,让我试试吗?这样我就有更大的机会找到问题!(分别从github下载main.lua和tween.lua代码似乎不起作用。)ps:另外,你使用的是什么love2d版本?@Isti115该项目位于github。可以在此处查看所有代码:。我的目标是仅在按住适当的按钮时向左或向右移动角色。我正在使用最新版本的爱。这是一个很好的解决方案,但我找到了一个更基本的方法。我没有重置,而是用负的DT更新tween。基本上,重置所做的就是将对象返回原点。只需使用-DT进行更新,X值就会开始缩小,并用更少的代码实现相同的效果。不过,谢谢你的回答,它完全实现了它所需要的。确认为答案。感谢您接受我的答案,并告知我您的解决方案!:)这比我所能做到的要聪明得多