External Exam Download Resources Web Applications Games Recycle Bin

GML - Ladders



Requires Collision Correction to be completed (but not any questions or extension activities), as this activity continues on from the end of that example.



in addition to the sprites used in Collision Correction:

SpriteImages
sprLadder

in addition to the objects used in Collision Correction:

ObjectSpriteProperties
objLaddersprLadderVisible

same room layout as Collision Correction but this time add a couple of objLadder ladders:

//pre-existing code before this
tethered = false; //attached to a ladder?
climb_speed = 2;
//in addition to pre-existing code:

if place_meeting(x, y, objLadder)
{ 
  if (keyboard_check(vk_up) or keyboard_check(vk_down))
    { tethered = true; } 
} else { tethered = false; }

// add constant force of gravity to our desired vert speed:

if not tethered{
  vert = vert + grav;
} else {
  // tethered climbing:
  if (keyboard_check(vk_up)) vert = -climb_speed;
  if (keyboard_check(vk_down)) vert = +climb_speed;
  if (keyboard_check(vk_nokey)) vert = 0;
}
grav = 1; //our gravity
horiz = 0; //our desired horizontal speed
vert = 0; //our desired vertical speed
move_speed = 5;
jump_speed = -12;
frict = 0.95;
facing = 1; //start facing right
jumped = false; //controls jump animations
status = ""; //status of platform "stick"
tethered = false; //attached to a ladder?
climb_speed = 2;
// left and right movement inputs AND animations:

if (keyboard_check(vk_right)){ horiz = move_speed; facing = 1; };
if (keyboard_check(vk_left)){ horiz = -move_speed; facing = -1; };
if (horiz != 0 and not jumped)
  { sprite_index = sprWalk; }
if (keyboard_check(vk_nokey)){
	horiz = horiz * frict; //friction
	if (abs(horiz) < 1 and abs(vert) < 1) { sprite_index = sprStill };
}; 

if place_meeting(x, y, objLadder)
{ 
  if (keyboard_check(vk_up) or keyboard_check(vk_down))
    { tethered = true; } 
} else { tethered = false; }

// add constant force of gravity to our desired vert speed:

if not tethered{
  vert = vert + grav;
} else {
  // tethered climbing:
  if (keyboard_check(vk_up)) vert = -climb_speed;
  if (keyboard_check(vk_down)) vert = +climb_speed;
  if (keyboard_check(vk_nokey)) vert = 0;
}


// only ALLOW jump IF the objBlock is 1 pixel BELOW the player,
// or in other words, if the player is EXACTLY standing on floor:

if (place_meeting(x, y+1, objBlock) and keyboard_check(vk_up))
  { vert = jump_speed; jumped = true; } 
  
// jump animations:

if (jumped) {
	// up down animations:
	if (vert > 0) { sprite_index = sprJumpUp; } 
    else { sprite_index = sprLanding; }
	// reset jump IF on ground again:
	if (place_meeting(x, y+1, objBlock)) { jumped = false; }
}

// check for a HORIZONTAL collision:

if (place_meeting(x+horiz, y, objBlock))
{
    // move AS CLOSE AS WE CAN to the HORIZONTAL collision object:
	// sign() can only return 1, -1, or 0.
	// sign(-7) == -1 (i.e. negative 1)
	// sign(7) == 1 (or +1 or positive 1)
	// sign(0) == 0
	
    while (!place_meeting(x+sign(horiz), y, objBlock))
    {
        x = x + sign(horiz); // move 1 pixel at a time towards object
    }
	
    horiz = 0; // no left right movement on HORIZONTAL collisions 
}

// we have worked out our horizontal speed, now APPLY the change:

x = x + horiz;

// check for a VERTICAL collision:

if (place_meeting(x, y+vert, objBlock))
{
    // move AS CLOSE AS WE CAN to the VERTICAL collision object:
	
    while (!place_meeting(x, y+sign(vert), objBlock))
    {
		// remember, sign() can only return 1, -1, or 0:
        y = y + sign(vert); // // move 1 pixel at a time towards object
    }
	
    vert = 0; // no up down movement on VERTICAL collisions 
}

// we have worked out our vertical speed, now APPLY the change:

y = y + vert;

experiment with platform traversal and moving platforms