Log In  
Follow
Huntsmouse
[ :: Read More :: ]

I'm not sure if I chose the right category for this :)

Anyways, I made a simple yet useful pingpong-value function. It's useful for animating sprites back and forth in a loop. I decided to share it with you - feel free to use and modify it for any of your needs!

//PingPong value
//-----------------------------
//Loops x from a to b and
//then from b to a (inclusive)
function pingpong(x,a,b)
  local d=b-a
  local p=x%(d*2)
  if p>d then
    p=2*d-p
  end
  return p+a
end

//example usage
//-----------------------------
frame = 0

function _update()
  frame+=1
end

//constantly animates sprite from
//16 to 24 and back from 24 to 16
function _draw()
  spr(pingpong(frame,16,24),58,58)
end
P#28304 2016-09-11 08:17 ( Edited 2016-09-11 12:17)