Simple movement display doesn´t work (ladder-climbing - (SOLVED!))

Hi Folks.

Here is a very simple code-block for ladder-climbing.
My game hero is working absolutely fine. I made further idles
for climbing a ladder. He climbs up and down, as I wanted.
But: He doesn´t play the full loop of the climb-idle "sPlayerLadderClimb".
You can only see the first image of the sprite, but not the following 13 images (animation).

I tried different ways to solve it, bit nothing helped. Can anybody
help me with having a look on the code, below?
Is there a way to run the full loop?



Code:
//LadderClimb
if (place_meeting(x,y,o_Ladder))
{
    if (key_up)
    {
        vsp = -4;
        sprite_index = sPlayerLadderClimb;
        image_index = 0;
        image_speed = 1

    }
    if (key_down)
    {
        vsp = 4;
        sprite_index = sPlayerLadderClimb;
        image_index = 0;
        image_speed = -1
    }
}
Thank you a lot, previously, for your help!
Sincerely,
Archie
 
Every step is setting the image_index to 0, so it doesn't matter what the image_speed is, it won't progress past the first frame. Depending on your climbing animation, you could just remove image_index = 0 from the code entirely, or only set it as you change the state rather than within the state itself.
 

chorrus

Member
image_index = 0;

This is being executed everytime you are pressing key up or key down. That is the reason why it only shows the first sprite. Remove that and it should be fixed.
 
Thank you both...
...but I tried it before.
I guess there is something in trouble with the codelines above // Ladder Climb (look below).
I´ve put the whole Step-Code here.

I tried and tried... I´m not experienced enough to see the working clash in it.
What do the experts say?

Code:
// Get Player Input
key_left=keyboard_check(vk_left)
key_right=keyboard_check(vk_right)
key_down=keyboard_check(vk_down)
key_up=keyboard_check(vk_up)
key_jump=keyboard_check_pressed(vk_space);

// Calculate Movement
var move = key_right - key_left;

hsp = move * walksp;

vsp = vsp + grv;

if(place_meeting(x,y+1,oWall)) && (key_jump)
{
    vsp = -9;
    audio_play_sound(au_Jump,5,false);
}
    
// Horizontal Collision
if(place_meeting(x+hsp,y,oWall))
{
    while(!place_meeting(x+sign(hsp),y,oWall))
    {
        x = x + sign(hsp);
    }
    hsp = 0;
}
x = x + hsp;

// Vertikal Collision
if(place_meeting(x,y+vsp,oWall))
{
    while(!place_meeting(x,y+sign(vsp),oWall))
    {
        y = y + sign(vsp);
    }
    vsp = 0;
}
y = y + vsp;

if (hsp != 0) image_xscale = sign (hsp);

//Animation speed
if (!place_meeting(x,y+1,oWall))
{
    sprite_index = sPlayerA;
    image_speed = 0;
    if (sign(vsp) > 0) image_index = 1; else image_index = 0;
}
else
{
    image_speed = 1;
    if (hsp == 0)
    {
        sprite_index = sPlayer;
    }
    else
    {
        sprite_index = sPlayerR;
    }
}

//Ladder Climb
if (place_meeting(x,y,o_Ladder))
{
    if (key_up)
    {
        vsp = -4;
        sprite_index = sPlayerLadderClimb;
        image_speed = 1

    }
    if (key_down)
    {
        vsp = 4;
        sprite_index = sPlayerLadderClimb;
        image_speed = -1
    }
}

Thanks for your patience with a novice.
Regards,
Archie.
 
G

Guitarmike

Guest
I doubt this matters, but in both branches of the ladder code you are missing a semi-colon after image_speed=1
 
I doubt this matters, but in both branches of the ladder code you are missing a semi-colon after image_speed=1
Sorry. But that doesn´t solve that problem. I found out, that in that case, a missing semi-colon seem to have no malfunction effect of its presence... to set the image speed.
I don´t know, why. But be it as it may... I still have that told problem of a frozen ladder-climber. :-(

I think, I must find the devil in the detail.
Thank you all for your help!

Have a great Xmas time.
Archie
 

Slyddar

Member
Try to structure the code in your player like this:
  • Capture input
  • Calculate movement
  • Apply movements/collisions
  • Apply animation
That way you are applying all the animations for the movements that you applied, after you have calculated them. I'm pretty sure you know this already, as you almost have this setup, but you are trying to do movement and animation after the movement has happened, and that can be problematic.

Also it's probably best to capture when you are on the ladder as a variable, and you can then use it throughout the code to help work out where the player is. Same with being on the ground. I've made some adjustments to make it work. As it stood, you never zeroed the vsp when not climbing, so it would never stop, unless that is by design. Also you only want grav to be applied when not on the ladder. Basically I just added some checks, and then did the logic in the animation section to ensure we get the right sprite depending on our location.

Code:
/// @description
// Get Player Input
key_left=keyboard_check(vk_left)
key_right=keyboard_check(vk_right)
key_down=keyboard_check(vk_down)
key_up=keyboard_check(vk_up)
key_jump=keyboard_check_pressed(vk_space);

// Calculate Movement
var move = key_right - key_left;

hsp = move * walksp;

//checks
if (place_meeting(x,y+1,oWall)) var on_ground = true else var on_ground = false;
if (place_meeting(x,y,o_Ladder)) var on_ladder = true else var on_ladder = false;

//apply grav if not on ladder
if !on_ladder vsp = vsp + grv;

//Ladder Climb
if on_ladder
{
    vsp = 0;
    if (key_up) vsp = -4;
    if (key_down) vsp = 4;
}

if (on_ground) && (key_jump)
{
    vsp = -9;
    audio_play_sound(au_Jump,5,false);
}
 
// Horizontal Collision
if(place_meeting(x+hsp,y,oWall))
{
    while(!place_meeting(x+sign(hsp),y,oWall))
    {
        x = x + sign(hsp);
    }
    hsp = 0;
}
x = x + hsp;

// Vertikal Collision
if(place_meeting(x,y+vsp,oWall))
{
    while(!place_meeting(x,y+sign(vsp),oWall))
    {
        y = y + sign(vsp);
    }
    vsp = 0;
}
y = y + vsp;

if (hsp != 0) image_xscale = sign (hsp);

//Animation speed
if on_ladder{
    if on_ground {
        //on_ground and on_ladder
        image_speed = 1;
        if hsp == 0 sprite_index = sPlayer; else sprite_index = sPlayerR;
    } else {
        //Not on_ground but on_ladder
        if vsp == 0 image_speed = 0 else image_speed = 1;
        sprite_index = sPlayerLadderClimb;
    }
} else {
    //not touching ladder
    if !on_ground
    {
        //not touching ladder and not on_ground
        sprite_index = sPlayerA;
        image_speed = 0;
        if (sign(vsp) > 0) image_index = 1; else image_index = 0;
    }
    else
    {
        //not touching ladder and on_ground
        image_speed = 1;
        if (hsp == 0) sprite_index = sPlayer; else sprite_index = sPlayerR;
    }
}
Also to answer your original problem, this was the area causing the issue.
Code:
if (!place_meeting(x,y+1,oWall))
{
   sprite_index = sPlayerA;
   image_speed = 0;
   if (sign(vsp) > 0) image_index = 1; else image_index = 0;
}
When you are on the ladder this is true, meaning you were always setting the image_index to 0 or 1 each step.
 
Last edited:
You wrote:

When you are on the ladder this is true, meaning you were always setting the image_index to 0 or 1 each step:

Code:
Also to answer your original problem, this was the area causing the issue.
Code:

if (!place_meeting(x,y+1,oWall))
{
   sprite_index = sPlayerA;
   image_speed = 0;
   if (sign(vsp) > 0) image_index = 1; else image_index = 0;
}

Thank you, Sly!
:eek:
Now I understand the connections. That makes absolutely sense!
You´re right - I was busy with writing all codes down, I learned, but I have no order in structure.
I knew that before - I just wanted to clear taht up, after everything works. But now I recognize,
that the funcionality is also depeding from senseful structure.

A good lesson. :)
Thanks a lot!

I will try to solve that problem and I report about, when it is fixed.
Have nice Xmas-days. ;)

Archie
 
Post scriptum to The Sly:

It works, perfect!!! :):):)
You gave me a tidy code, simply to understand.
I will look for such a structure, futurely.
Wavywave! :D

Archie
 
Top