【问题标题】:Love2D, LUA Tween resettingLove2D,LUA Tween 重置
【发布时间】:2016-04-21 23:12:44
【问题描述】:

我的补间有问题。按住该按钮时,我正在使用 tween.lua 向左或向右移动我的角色。释放时,播放器返回中间。当角色向左或向右移动时,补间非常有效,但是由于某种原因,当它必须回到中间时,角色只是在那里扭曲而不是补间。我怀疑基础 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

【问题讨论】:

  • 我试图对此进行调查,但我认为我没有完全理解您试图实现的目标。您能否将整个项目以 zip 的形式发布,以便我试一试?这样我就有更大的机会找到问题! (从github上分别下载这个代码为main.lua和tween.lua似乎不起作用。) ps.:另外,你使用什么love2d版本?
  • @Isti115 该项目托管在 GitHub 上。所有代码都可以在那里查看:github.com/meganukebmp/At-the-speed-of-light。我的目标是仅在按住相应按钮时向左或向右移动角色。我用的是最新版的love。

标签: lua tween love2d


【解决方案1】:

代码的工作版本

所以,在把代码弄乱了很多之后,我可以向你展示以下内容:

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

ps.:我不介意的另一件事是,如果您可以将源代码中 cmets 的措辞保持在良好品味的范围内。 [khm..debugging.lua : line 7.khm..]

希望您的项目取得成功!祝你好运!

【讨论】:

  • 这是一个很好的解决方案,但我想出了一个更基本的方法。我没有重置,而是用负 DT 更新补间。基本上所有重置都是将对象返回到原点。通过使用 -DT 更新,X 值开始按比例缩小并以更少的代码实现相同的效果。不过,感谢您的回答,它完全满足了它的需要。确认为答案。
  • 感谢您接受我的回答并告知我您的解决方案! :) 它比我能够实现的要聪明得多! ;)
猜你喜欢
  • 2021-05-24
  • 2020-07-19
  • 2020-01-03
  • 2018-10-30
  • 2014-06-10
  • 2021-12-13
  • 2018-05-22
  • 2014-07-22
  • 2021-03-11
相关资源
最近更新 更多