• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Problems with platformer animation.

S

SoMalFra

Guest
I've been trying to do an Animation code for the player that works with my current movement code. It's kind of a mess at the moment, so i've decided to try a new one instead of trying to fix it, because it would probably take way too much effort, that could be evaded doing something new. The code at the moment has some bugs I just can't find a way to fix. As an example, if i'm touching a wall and press a button to look to the other side, it just instantly snaps me to a wall on the opposite way. If there's no wall there, the game just totally freezes, and i can't even close it the normal way. The code i'm using currently is the following:

Script "Move"
GML:
///Script "move"

var speed_ = argument0;
var bounce_ = argument1;

if place_meeting(x+speed_[h], y, oWall) {
    while !place_meeting(x+sign(speed_[h]), y, oWall) {
        x += sign(speed_[h]);
    }
    if bounce_ > 0 {
        speed_[@ h] = -speed_[@ h]*bounce_;
    } else {
        speed_[@ h] = 0;
    }
}
x += speed_[h];


if place_meeting(x, y+speed_[v], oWall) {
    while !place_meeting(x, y+sign(speed_[v]), oWall) {
        y += sign(speed_[v]);
    }
    if bounce_ > 0 {
        speed_[@ v] = -speed_[@ v]*bounce_;
    } else {
        speed_[@ v] = 0;
    }
[CODE]move(speed_, nothing);


var hinput = keyboard_check(vk_right) - keyboard_check(vk_left);

if hinput != 0 {
    speed_[h] += hinput*acceleration_;
    speed_[h] = clamp(speed_[h], -max_speed_, max_speed_);


} else {
    speed_[h] = lerp(speed_[h], 0, friction_);
;
}

if !place_meeting(x, y+1, oWall) {
    speed_[v] += grv;
}
y += speed_[v];
}

Step event object "oPlayer" (the movement part)

Code:
Code:
move(speed_, nothing);


var hinput = keyboard_check(vk_right) - keyboard_check(vk_left);

if hinput != 0 {
speed_[h] += hinput*acceleration_;
speed_[h] = clamp(speed_[h], -max_speed_, max_speed_);


} else {
speed_[h] = lerp(speed_[h], 0, friction_);
;
}

if !place_meeting(x, y+1, oWall) {
speed_[v] += grv;
}

step event object "oPlayer" (the animation part)



if (!place_meeting(x,y+1, oWall))
{
sprite_index = sPlayerA;
image_speed = 0;
if (sign(speed_[h]) > 0) image_index = 1; else image_index = 0
}
else
{
image_speed = 0.1;
if (speed_[h] == 0)
{
sprite_index = sPlayer;
image_speed = 0.05
}
else
{
sprite_index = sPlayerR;
}
}

if (speed_[h] != 0) image_xscale = sign(speed_[h]);

if key_down
{
sprite_index = sPlayerD
}

if key_up
{
sprite_index = sPlayerU
}



Create event (oPlayer)
Code:
speed_ = [0,0]
grv = 0.25;


jumpsMax = 2
 jumps = 2                 
 
jumpspeed_normal = 3.5;
jumpSpeed = jumpspeed_normal

firingdelay = 0;

max_speed_ = 1.5;

nothing = 0
acceleration_ = .3;
friction_ = .3



If ANYTHING else is needed, just ask me and i'll post. Thank you very much.
 

Slyddar

Member
Try setting the player to a single mask, so that swapping to different sprites doesn't put you in a collision, because the masks are different sizes. There is an option to set the Collision mask to one particular sprite in the object properties.
 
S

SoMalFra

Guest
Try setting the player to a single mask, so that swapping to different sprites doesn't put you in a collision, because the masks are different sizes. There is an option to set the Collision mask to one particular sprite in the object properties.
I've done this. I really don't think this is the problem. It's not the player that gets stuck in the wall, but rather the whole game. I recorded a shor video that shows what happens:
 

Slyddar

Member
Are you flipping the player using image_xscale? Try creating a 'facing' variable to hold the direction he is facing, then use draw_sprite_ext() in a draw event to draw the sprite with the image_xscale argument set to the 'facing' variable.
 
S

SoMalFra

Guest
Side note:
I think you meant to use abs() instead of sign() there. Or even just

if speed_[h] != 0

Or you meant to use speed_[v], since checking horizontal vectors in air is kinda pointless.
That actually solved another problem I had with animation, so thank you lol. However, I'm still not able to figure why that bug that just freezes my game is happening. Looking a bit more into it, I thnk it could be an infinite while loop, so i'll try rewriting all my code, though i still don't know what i did wrong.
 
Top