Discuss and collaborate on GML scripts

You are not logged in.

- Topics: Active | Unanswered

Pages: **1**

**Odolwa****Member**- Registered: 2013-07-26
- Posts: 4

Hello, this is my 1st time posting!! I have a problem that will require a bit of explaining, so please bear with me.

Basically, I'm putting together a game similar in style to 'Thomas Was Alone' &, as such, my character sprite, wall and ladder instances are all 32x32 squares. Below are the parts of my code for climbing a ladder and detecting horizontal & vertical collisions.

When I tested the code initially, the character would get stuck on ladders next to walls when facing to the left. I then added the code, highlighted in blue, at the beginning of the vertical collision script and this corrected the problem. Unfortunately, it then created a new problem wherein the character will continue climbing down into the floor or up into the ceiling if down or up are held, respectively.

I was told that I need to use some code that will check when the sprite has reached the floor or ceiling, rather than check for a collision. As I'm new to coding (most of what you see here is from online tutorials), I'm at a loss as to how to go about doing this. The code in red was the closest I've gotten, however it just brings me back to my original problem of getting stuck on the ladder. Can anyone please help me out?

Many thanks.

//Ladder

if (Key_Up || Key_Down)

{

if place_meeting(x,y,par_ladder) ladder = true;

}

if (ladder)

{

if (Key_Up && (Key_Right || Key_Left || Key_Jump)) || !place_meeting(x,y,par_ladder)

ladder = false;

else

{

vsp = 0;

hsp = 0;

if (Key_Up) vsp = -2;

if (Key_Down) vsp = 2;

if !place_meeting(x,y,par_ladder) ladder = false;

if (Key_Up && (Key_Right || Key_Left || Key_Jump)) ladder = false;

if (Key_Jump) ladder = false;

if (Key_Right) ladder = false;

if (Key_Left) ladder = false;

//Centre Sprite on Ladder

closest_ladder = instance_nearest(x,y,par_ladder)

x = closest_ladder.x+(closest_ladder.sprite_width/2);

}

}

//Horizontal Collision

if place_meeting(x+hsp,y,par_wall)

{

while (!place_meeting(x+sign(hsp),y,par_wall)) x+=sign(hsp);

hsp = 0;

}

x += hsp;

//Vertical Collision

if !(ladder);

else

{var inst;

inst = instance_place(x, y, par_wall)

if inst != noone

{grounded = 1

}

}

if place_meeting(x,y+vsp,par_wall)

{

while (!place_meeting(x,y+sign(vsp),par_wall)) y+=sign(vsp);

if (sign(vsp) == 1)

{

grounded = 1;

djump = 1;

}

vsp = 0;

}

else

{

grounded = 0;

}

y += vsp;

*Last edited by Odolwa (2013-07-26 19:58:30)*

Offline

**Odolwa****Member**- Registered: 2013-07-26
- Posts: 4

Ok, I got some help on this one from a different source, so I'll update the thread, here.

It turns out that image_xscale = -1, during a left turn, was causing the collision with wall instances when applied to ladder climbing. Here's the code with image_xscale:

//Move left and right

if (Key_Left)

{

hsp = -6;

image_xscale = -1;

}

if (Key_Right)

{

hsp = 6;

image_xscale = 1;

}

As such, I needed to set image_xscale to 1 when on the ladder - highlighted in red:

//Ladder

if (Key_Up || Key_Down)

{

if place_meeting(x,y,par_ladder) ladder = true;

}

if (ladder)

{

if (Key_Up && (Key_Right || Key_Left || Key_Jump)) || !place_meeting(x,y,par_ladder)

ladder = false;

else

{

vsp = 0;

hsp = 0;

image_xscale = 1

if (Key_Up) vsp = -2;

if (Key_Down) vsp = 2;

if !place_meeting(x,y,par_ladder) ladder = false;

if (Key_Up && (Key_Right || Key_Left || Key_Jump)) ladder = false;

if (Key_Jump) ladder = false;

if (Key_Right) ladder = false;

if (Key_Left) ladder = false;

//Centre Sprite on Ladder

closest_ladder = instance_nearest(x,y,par_ladder)

x = closest_ladder.x+(closest_ladder.sprite_width/2);

}

}

Hope this helps anyone who encounters the same problem. Good luck.

Offline

**xot****Administrator**- Registered: 2007-08-18
- Posts: 1,201

OK, that was easy. Welcome to the forums.

*Abusing forum power since 1986.*

Offline

Pages: **1**