GML sprite issues

sweep

Member
Hope you can help,it's probably an easy fix but I can't find a solution. My player isn't switching to the correct sprite when I am climbing. The code is below. The player changes to its falling sprite while going up or down a ladder. Is the jumping and falling code overwriting the climbing ?

ANIMATION
image_speed = 1;
if (hspd !=0) image_xscale = sign(hspd);
if (!onground)
{
if (onwall != 0)
{
sprite_index = spr_player_wall;
image_xscale = onwall;

var _side = bbox_left;
if (onwall == 1) _side = bbox_right;
dust++
if ((dust > 2) && (vspd > 0)) with (instance_create_layer(_side,bbox_top,"player",obj_dust))
{
other.dust = 0;
hspeed = -other.onwall *0.5;
}

}
else
{
dust = 0;
sprite_index = spr_player_air;
image_speed = 0;
image_index = (vspd > 0);
}


}
else
{
if (hspd != 0 ) sprite_index = spr_player_run; else sprite_index = spr_player;
}

STATE CHANGE
if (key_up || key_down)
{
if (place_meeting(x,y,obj_climb)) ladder = true;
}

if (ladder)
{
scr_onladder();
}



ONLADDER script
function scr_onladder()
{

if (ladder)
{
vspd = 0;
if (key_up)
{
sprite_index = spr_player_climb;
image_index = 0;
image_speed = 1;
vspd = -2;
}

if (key_down)
{
sprite_index = spr_player_climb;
image_index = 0;
image_speed = 1;
vspd = 2
}

if (!place_meeting(x,y,obj_climb)) ladder = false;
if (key_jump) ladder = false;
}

}
 

sweep

Member
Yes, you should be checking your ladder variable before going into the parts that change the sprite.
I added ladder = true at the top of the ladder script. Didn't change the sprite. I'll get my head around this one day.

{

if (ladder)
{
vspd = 0;
ladder = true;
if (key_up)
{
sprite_index = spr_player_climb;
image_index = 0;
image_speed = 1;
vspd = -2;
}

if (key_down)
{
sprite_index = spr_player_climb;
image_index = 0;
image_speed = 1;
vspd = 2
}

if (!place_meeting(x,y,obj_climb)) ladder = false;
if (key_jump) ladder = false;
}

}
 

Nidoking

Member
if (ladder)
{
vspd = 0;
ladder = true;
This is not what I meant. If ladder is true, you set ladder to true. Meanwhile, in the parts where you change the sprite to sprites other than the climbing sprite, you have failed to tell it not to do that if ladder is true.
 

TheouAegis

Member
Yes, ladder is true. What does that actually mean? Right now, all it means to your code is pressing up or down changes the sprite to a ladder climbing sprite. So when you aren't pushing up or down, there us nothing telling the instance to change to a climbing sprite, so on the next step the game detects there is no ground below and changes to the falling sprite. Do you want it to change to the falling sprite while on a ladder? No. So logically, you should check that you aren't on a ladder before falling.
 

sweep

Member
Really sorry I am lost. I've tried adding the variable but it messed up the gravity on the ladder.
 

Nidoking

Member
I'm going to make this really simple, because you don't seem to grasp basic ideas.

Your code marked "Animation" in your original post changes the sprite_index in several places. Do you see that?

The problem you have is that, when you're on the ladder, the sprite is changing and you do not want it to change. Is that clear?

You either seem to be familiar with "if", or you have copied everything you've done from somewhere and not bothered to try to understand it. "if" means that you want something to happen under certain conditions, but not under other conditions.

In this case, the thing that you want to sometimes happen and sometimes not happen is for the sprite to change. As I mentioned previously, that's the part you called "Animation". I say this again because that was a few sentences ago and you've probably forgotten by now.

When do you want it to happen? When you're not on a ladder. When do you want it to not happen? When you're on a ladder.

You have a variable called "ladder" that tells you when you're on a ladder.

If ladder is true, then you're on a ladder. In this case, you don't want the sprite to change, because you're on a ladder.

If ladder is false, then you're not on a ladder, and you want to do the other things that may result in the sprite changing.

If ladder is false, then !ladder is true.

If !ladder is true, then you want to do the Animation part of your code.

If !ladder is false, then you don't want to do the Animation part of your code, because you're on a ladder, and the Animation part of your code would result in the sprite changing to a not climbing the ladder sprite.

Please tell me you have figured out what to do by now.
 

sweep

Member
It might be basic to you and yes I did copy because I am learning. I did bother to read up about what "if" means. What I don't know is where in the code I've gone wrong so i ve asked for help. No need to talk down to me and be patronizing.
 

Nidoking

Member
If you don't understand what you've "written", then don't write it. Copying is only as useful as your ability to understand what you've copied. What you're doing is not learning.

When you use "if", you also follow it with an open curly brace ( { ). From this point until the matching close curly brace ( } ), everything between the braces only happens if the thing in the "if" is true. If the thing in the "if" is false, then everything between the braces ( { } ) doesn't happen. So, the sequence is: if, the thing you're testing to see if it's true or not, open curly brace ( { ), the things you only want to happen if the first thing was true, close curly brace ( } ).

The thing you want to test is !ladder. The ! means "not", and it means true if the thing after it is false, false if the thing after it is true. You want ladder to be false (you are not on a ladder), so you want !ladder to be true (it is not true that you are on a ladder).

The thing you want to happen only if !ladder is true is everything you have under Animation. That's where you're changing the sprite.
 
Top