Log In  


when I press the left and right arrows, my sprite plays an animation that starts from sprite 1 to sprite 4, since the init function reports that sp=1.At the same time, there is a standard check if SP <4, then.However, I cannot reproduce the animation of sprites from 17 to 20 when I press the up arrow, because as soon as I say in the loop that SP = 17, the animated sprites overlap each other due to very fast sequential rendering.Could you please help fix this?Here is the code:
--player--
player={
x=63,
y=63,
f=false,
sp=1,
speed=1,
stimer=0
}
end

function _update()
--player movment--
if btn(➡️) then
player.x+=player.speed
player.f=fals

if player.stimer<10 then
player.stimer+=1
else
if player.sp<4 then
player.sp+=1
else
player.sp=1
end
player.stimer=0
end
end
if btn(⬅️) then
player.x-=player.speed
player.f=true

if player.stimer<10 then
player.stimer+=1
else
if player.sp<4 then
player.sp+=1
else
player.sp=1
end

player.stimer=0
end
end

if btn(⬆️) then
player.y-=player.speed
print(player.sp)
if player.stimer<10 then
player.stimer+=1
else
player.sp=17
if player.sp<20 then
player.sp+=1
else
player.sp=17
end
player.stimer=0
end
function _draw()
cls()
map()
spr(player.sp,player.x,player.y,1, 1,player.f)
end



The problem is that your code doesn't have a clear spot to set the initial animation frame, due to not keeping track of the previous direction of motion. What you can do instead is check both sides of the animation loop.

Instead of having two lines that sp to 17, take out the earlier one and make the condition if player.sp < 20 and player.sp >= 17 then .

One other thing, it's easier to read your code if you use the code block tags. that's ``` before and after the code.


Thanks a lot, it works



[Please log in to post a comment]