Love2D,LUA Tween重置

时间:2016-01-16 11:55:16

标签: lua tween love2d

我的补间问题。我正在使用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

1 个答案:

答案 0 :(得分:3)

代码的工作版本

因此,在使用代码搞砸了很多之后,我可以向您展示以下内容:

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.:如果你能在源代码中保持你的评论措辞在良好品味的界限之间,我不会介意的另一件事。 [khm .. debugging.lua:line 7. khm ..]

我希望你的项目成功!祝你好运!