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
|
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.
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.
I tried both solutions but it doesnt seem too work. I really don't know what to do
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)
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!
[Please log in to post a comment]



