Skidding animation

Joltout

Member
So basically, I'm trying to get my character to slide in their skidding animation/image_index to a stop when either no directional keys are pressed (or both at the same time), or when the player changes direction and their momentum is still carried over (for example, sliding them right when they're trying to move left).

And while I have the characters movement down fine, getting the animations for the sprite to work right is proving to be a pain in my rear. I've tried multiple ways of coding around it, but me being the newbie I am, I haven't found a solution. As far as I can understand, the code I've written prioritises the right side movement because it comes first in the code, so the animation works fine on the right, but when I move left, the character is just permanently in the sliding animation.

Any help? :(


Code:
if (place_meeting(x,y+1,oWall))
{
    if ((!hsp == 0) and (!key_right or !key_left))
    {
        sprite_index = N_Skid;
        image_index = 1;
    }
}
 

Joltout

Member
I tried that, and all that's changed is that now instead of sliding when moving left, my character just runs normally and doesn't slide at all

I really don't know what I'm doing wrong
 

TheouAegis

Member
try small change
if ((!hsp == 0) and (!key_right and !key_left))
!hsp == 0 is only true for moving right
hsp != 0 is true for both left and right movement


@OP Of you had something like
Code:
move = right-left;
hsp += move*accel
then you could check if sign(move) != sign(hsp).
 

Joltout

Member
@OP Of you had something like
Code:
move = right-left;
hsp += move*accel
then you could check if sign(move) != sign(hsp).
Code:
if (place_meeting(x,y+1,oWall))
{
    if sign(move) != sign(hsp)
    {
        sprite_index = N_Skid;
        image_index = 1;
       if (hsp == 0)
       {
           image_speed = 1;
           sprite_index = N_Idle;
       }
    }
}
That kind of worked, but the character is now stuck in an image_index = 1 idle sprite situation. Not sure how to get it back to an animation instead of a sprite lock, especially since I called the hsp == 0 index/speed earlier in its own block
 

TheouAegis

Member
Usually, your hsp isn't quite 0. What about checking if abs(hsp) is less than your acceleration speed? How are you accelerating/decelerating?
 

Joltout

Member
Code:
if (hsp < max_hsp) and (hsp > -max_hsp)
{
    hsp += (move * movespeed);
}
This is basically my acceleration, where max_hsp = 3, move = key_left + key_right and movespeed = 0.2
There's more to it than this obviously but that's the basics.


Code:
if (hsp > 0) && (key_left = 0) && (key_right = 0) && (place_meeting(x,y+1,oWall)) {hsp -= movespeed}
if (hsp > max_hsp) && (key_left = 0) && (key_right = 0) && (place_meeting(x,y+1,oWall)) {hsp -= movespeed}
The deceleration is basically a lot of if statements like these, alternating values depending on what keys are pressed, if !place_meeting, etc. A large wall of if statements in my code, seems messy and I'm sure there's a better way around it, but it works so I've kept it.

All of that aside, I get the feeling like I could be super direct about it and spam if statements again, but again, that feels like bad practice and could cause a lot of problems and tripping up in the future
 

TheouAegis

Member
if (hsp < max_hsp) and (hsp > -max_hsp)
if abs(hsp) < max_hsp


max_hsp = 3, move = key_left + key_right and movespeed = 0.2
The 0.2 is likely causing a floating point error. If you change it to 0.25, your code "if hsp==0" code should work. In either case, as I said, you should try using "if abs(hsp) < movespeed" instead of "if hsp==0" for that reason.


if (hsp > 0) && (key_left = 0) && (key_right = 0) && (place_meeting(x,y+1,oWall)) {hsp -= movespeed} if (hsp > max_hsp) && (key_left = 0) && (key_right = 0) && (place_meeting(x,y+1,oWall)) {hsp -= movespeed}
Code:
if abs(hsp) < max_hsp && place_meeting(x,y+1,oWall) {
    if move==0 {
        hsp -= movespeed*sign(hsp);} else {hsp += move*movespeed;
    }
 }
 

Joltout

Member
The 0.2 is likely causing a floating point error. If you change it to 0.25, your code "if hsp==0" code should work. In either case, as I said, you should try using "if abs(hsp) < movespeed" instead of "if hsp==0" for that reason.
Wait. So does that mean that I can't change the values of the variables without having a floating point error? Is there a way to fix this or revert it back down to the correct values by force? The values I have at the moment aren't set in stone, and any changes I do now just give off the previous problems as well (setting movespeed to 0.25 did fix it)

Edit: I've found a weird fix for the floating point error. Apparently, the value can equal -0. So I just created a separate block of code that's literally just this to fix the problem:


Code:
if hsp = -0
{
    hsp = 0;
}
 
Last edited:

TheouAegis

Member
Did you try the abs(hsp)<movespeed suggestion I gave you?
Be sure you test your hsp=-0 code with a bunch of other "divisible" speeds. Try a speed of 1/3. Try a speed of 3/13. Try a speed of 3/14.
 

Joltout

Member
Code:
if abs(hsp) < max_hsp
I did replace my code with this, yea. But the other bit didn't really work, since I'm assuming you just based it off of the 2 lines I gave and not the other, almost 20 lines, of similar if statements that're basically all the same aside from one variable changing each time.

And yea, the divisible speeds don't work unless I change the max_hsp variable to a divisible number of the changed movespeed. But otherwise, it all seems to work fine now
 
Top