【发布时间】: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。