Log In  
Log In  

Hi everyone! I'm using some functions to animate the player sprite, but they doesn't seem to work properly: the sprite changes just one time and then stay that way
EDIT: the sprite remains the first one and do not change

function player_up()
    player_frame=17
    player_y-=1
    if player_frame>18.9 then
        player_frame=17
    end
    player_frame+=1
end

function player_down()
    player_frame=1
    player_y+=1
    if player_frame>2.9 then
        player_frame=1
    end
    player_frame+=1
end

function _init()
    cls()
    player_frame=0
    player_x=30
    player_y=60
end

function _update()
    if btn(2) then  player_up() end
    if btn(3) then player_down()end
end

function _draw()
    cls()
    spr(player_frame,player_x,player_y)
end
P#55409 2018-08-20 07:31 ( Edited 2018-08-21 13:25)

:: Davbo

When you set your player frame to 1 you then immediately add 1 to it, making it 2 again. Then when it is more than 2 you set it to one, then add one making it 2 again. So when the function ends player_frame will never = 1 which I'm assuming is what you are trying to so. Same problem with the player up function except with 18.
Put the player_frame += 1 in an else clause or before the >2.9 check.

P#55410 2018-08-20 08:46 ( Edited 2018-08-20 12:46)

If your player sprite suddenly has too much coffee, you can try adding a decimal like 0.5 or 0.25 to your sprite value, and then have the draw function round it off.

P#55419 2018-08-20 11:07 ( Edited 2018-08-20 15:27)

I tried both solutions but it doesnt seem too work. I really don't know what to do

P#55421 2018-08-20 12:41 ( Edited 2018-08-20 16:41)
function player_up()
    player_y-=1
    player_frame=min(player_frame+1,17)
end

function player_down()
    player_y+=1
    player_frame=max(player_frame-1,1)
end

function _init()
    cls()
    player_frame=1
    player_x=30
    player_y=60
end

function _update()
    if btn(2) then  player_up() end
    if btn(3) then player_down()end
end

function _draw()
    cls()
    spr(player_frame,player_x,player_y)
end

That should do (but a bit difficult to know as we don’t have your sprite layout)

P#55424 2018-08-20 14:37 ( Edited 2018-08-20 18:37)
actor={}
actor.x=40
actor.y=30
actor.sprt=0
actor.spd=0.75

function move_down()
    actor.sprt+=0.125
    if actor.sprt>2.9 then
     actor.sprt=1
    end
end

The problem was that I set the sprite at the beginning of the function, so it stayed the same.
Thanks anywway!

P#55449 2018-08-21 08:08 ( Edited 2018-08-21 12:08)
:: Davbo

Oh - ding! Didn't clock that. Glad you fixed it!

P#55455 2018-08-21 09:25 ( Edited 2018-08-21 13:25)

[Please log in to post a comment]

About | Contact | Updates | Terms of Use
Follow Lexaloffle:        
Generated 2019-04-25 22:29 | 0.020s | 2097k | Q:28