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

Hello together

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 "punching" 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 :)

function _init()
    player={
        sp=1,
        x=59,
        y=59,
        anim=0,
        state="idle",
        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 == "idle" then
        idle()
    elseif player.state == "left" then
        printh("left")
    elseif player.state == "right" then
        printh("right")
    elseif player.state == "punching" then
        punching()
    elseif player.state == "defending" then
        printh("defending")
    end
end

function idle()
    if player.sp > 2 then
        player.sp = 1
    elseif time() - player.anim> .5 then
        player.anim=time()
        player.sp+=1
        if player.sp>2 then
            player.sp=1
        end
    end
end

function punching()
    if player.sp < 17 
    or player.sp > 19 then
        player.sp = 17
    elseif time() - player.anim> .1 then
        player.anim=time()
        player.sp+=1
        if player.sp > 19 then
            player.sp = 17
        end
    end
end
P#75555 2020-04-28 13:42

Follow Lexaloffle:          
Generated 2024-03-29 12:55:40 | 0.060s | Q:5