weitnow [Lexaloffle Blog Feed]https://www.lexaloffle.com/bbs/?uid=44094 Question, concerning code to switch animation <p>Hello together</p> <p>I just started with pico8, lua and game-programming. I am trying to make a small fighting game and have a question concering animations. I want the player-sprite to be idle and if i click a button it should respond instantly and play an animation. Is there a way I could write the function &quot;punching&quot; with less code? Sprite Number 17, 18 and 19 are the sprites for a punching animation. The function first checks if the current sprite is between 17 or 19...if not it sets it to 17 (start of the punching animation)...then it iterats through the animation and checks again if the sprite is greater then 19 to set it back to 17. I have the feeling I have to much code in it...especially that I check twice if player.sp is greater then 19. Is there a more efficient way? Thanks in advance :)</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>function _init() player={ sp=1, x=59, y=59, anim=0, state=&quot;idle&quot;, flp=false, } end function _update() set_state() player_animate() end function _draw() cls() spr(player.sp, player.x, player.y, 1, 1, player.flp) end function player_animate() if player.state == &quot;idle&quot; then idle() elseif player.state == &quot;left&quot; then printh(&quot;left&quot;) elseif player.state == &quot;right&quot; then printh(&quot;right&quot;) elseif player.state == &quot;punching&quot; then punching() elseif player.state == &quot;defending&quot; then printh(&quot;defending&quot;) end end function idle() if player.sp &gt; 2 then player.sp = 1 elseif time() - player.anim&gt; .5 then player.anim=time() player.sp+=1 if player.sp&gt;2 then player.sp=1 end end end function punching() if player.sp &lt; 17 or player.sp &gt; 19 then player.sp = 17 elseif time() - player.anim&gt; .1 then player.anim=time() player.sp+=1 if player.sp &gt; 19 then player.sp = 17 end end 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=37637 https://www.lexaloffle.com/bbs/?tid=37637 Tue, 28 Apr 2020 13:42:55 UTC