Huntsmouse [Lexaloffle Blog Feed]https://www.lexaloffle.com/bbs/?uid=15463 PingPong integer value (from a to b to a) <p>I'm not sure if I chose the right category for this :)</p> <p>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!</p> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre> //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&gt;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 </pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> https://www.lexaloffle.com/bbs/?tid=27631 https://www.lexaloffle.com/bbs/?tid=27631 Sun, 11 Sep 2016 08:17:25 UTC