Love2D, LUA Tween resetting -


i have problem tweening. using tween.lua move character left or right when button held. on release player returns middle. tweening works when character goes either left or right but, reason when has go middle character warps there , not tween. suspect either base x overriding or not resseting @ right moment. here code:

--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 , sets 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 , sets variable  if leftstarted , not isleft     goleft:reset()     leftstarted = false end if rightstarted , not isright     goright:reset()     rightstarted = false end if midstarted , not ismid     gomid:reset()     midstarted = false end   if ismid --if true action     gomid:update(dt)     midstarted = true end if isright --if true action     goright:update(dt)     rightstarted = true end if isleft --if true action     goleft:update(dt)     leftstarted = true end  if lk.isdown("left", "a") , not isright --this check needs done since code executed first time. if not check weird stuff happens     isleft = true      isright, ismid = false elseif lk.isdown("right", "d") --checks if button down , returns true if     isright = true     isleft, ismid = false else -- if nothing down resets player mid , variables 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 @ x , y end 

working version of code

so, after messing lot code, can present following you:

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 , sets 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 , sets variable      if leftstarted , not isleft     gomid = tween.new(player.speed , player, {x=player.mid}, 'linear')     leftneedsreset = true         leftstarted = false     end     if rightstarted , not isright     gomid = tween.new(player.speed , player, {x=player.mid}, 'linear')     rightneedsreset = true         rightstarted = false     end      if ismid --if true action         gomid:update(dt)     end     if isright --if true action     if rightneedsreset       goright:reset()       rightneedsreset = false     end         goright:update(dt)         rightstarted = true     end     if isleft --if true action     if leftneedsreset       goleft:reset()       leftneedsreset = false     end         goleft:update(dt)         leftstarted = true     end      if lk.isdown("left", "a") , not isright --this check needs done since code executed first time. if not check weird stuff happens         isleft = true         isright, ismid = false, false     elseif lk.isdown("right", "d") --checks if button down , returns true if         isright = true         isleft, ismid = false, false     else -- if nothing down resets player mid , variables 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 @ x , y end 

this sort of achieves think wanted (the player smoothly moving center), still don't think solution.
did made new motion appropriate starting point whenever player needs start moving towards center, , delayed resetting of directional motions until necessary, because makes player jump starting point.

observations

i have noticed few things while working code.

the first 1 tried give value multiple variables. far know, doesn't work way in lua. in order have write this:
isleft, isright = false, false
instead of this:
isleft, isright = false

also thing took while me notice while debugging have wrote resetting parts of directions in different order updating parts. wouldn't consider habit unless have got strong reasons way.

suggestions

in opinion library isn't suited task (although don't know well, first time using while debugging code). done built in functionality of language , framework itself. have variable keeps track of current position, , change continuously towards direction of pressed button until limit, , dragging center when keys released.

i haven't tested code, written blindly, might this:

if love.keyboard.isdown("left")   if currentposition > middleposition - movementlimit     currentposition = currentposition - 20 * dt   end elseif love.keyboard.isdown("right")   if currentposition < middleposition + movementlimit     currentposition = currentposition + 20 * dt   end else   if currentposition < middleposition     currentposition = currentposition + 20 * dt   elseif currentposition > middleposition     currentposition = currentposition - 20 * dt   end end 

ps.:another thing wouldn't mind if keep wording of comments in source code between bounds of taste. [khm.. debugging.lua : line 7. khm..]

i hope succeed project! luck!


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -