Moving Platforms

TheOnlyWRT

Member
Hey guys, so i am making a platformer game and the next feature i would like to add is moving platforms, both vertical and horizontal. I have already got jump through platforms working, and have used that as the basis. Horizontal platforms are working great, but i cant get vertical ones to work. When the player lands, he just falls right through the platform.

Here is the player collision code:
Code:
var vspFinal = vsp+vspCarry;
vspCarry = 0;

//check for a vertical collision before moving the player
var wall = place_meeting(x, y+vspFinal, objectFloorParent);
//if(Wall || (JumpThrough && vsp > 0 && bbox_bottom < JumpThrough.bbox_top)){
if(wall){ //this is a normal collision
    
    while(!place_meeting(x, y+sign(vspFinal), objectFloorParent)){
    
        y = y + sign(vspFinal);
    
    }
    vspFinal = 0;
    vsp = 0;
    
}


//check for a jump through platform
var jumpThrough = place_meeting(x, y + vspFinal, objectJumpThrough);
if(jumpThrough){ //this is a jumpthrough platform
    
    if((vspFinal >= 0) && (!place_meeting(x, y, objectJumpThrough))){ //we are moving down and should collide
        
        while(!place_meeting(x, y+sign(vspFinal), objectJumpThrough)){
    
            y = y + sign(vspFinal);
    
        }
        vspFinal = 0;
        vsp = 0;
    }
}
And here is the moving platform code:
Code:
//////////////////////
// MOVEMENT
//////////////////////
vsp = movesp*dir;


//check for a collision with an invisible block
if(place_meeting(x, y+vsp, objectFloorInvisible)){

    while(!place_meeting(x, y+sign(vsp), objectFloorInvisible)){
    
        y = y + sign(vsp);
    
    }
    dir = dir*-1;

}

//check for a vertical collision before moving the player
if(place_meeting(x, y+vsp, objectFloorParent)){

    while(!place_meeting(x, y+sign(vsp), objectFloorParent)){
    
        y = y + sign(vsp);
    
    }
    dir = dir*-1;

}

//move the player
y = y+vsp;


if(instance_exists(objectPlayer)){

    if(place_meeting(x, y-1, objectPlayer)){
    
        objectPlayer.vspCarry = vsp;
    
    }

}

Thanks!
 

TheouAegis

Member
Because the platform moves up into the player, so the player is no longer above the platform and so he falls through it; or the platform moves down and the player is no in contact with it and gravity doesn't affect the player until the next step, in which case the platform is below him but he won't collide with it.

Code:
//check for a jump through platform
if myPlatform == noone { //If you're not already on a platform
   var jumpThrough = instance_place(x, y + vspFinal, objectJumpThrough);
   if(jumpThrough){ //this is a jumpthrough platform
  
      if((vspFinal >= 0) && (!place_meeting(x, y, jumpThrough))){ //we are moving down and should collide
      
          vspFinal = 0;
          vsp = 0;

         myPlatform = jumpThrough;
      }
   }
}
else
{
   //Set the platform to noone if the player is no longer colliding with it
   if bbox_right < myPlatform.bbox_left || bbox_left > myPlatform.bbox_right || vspfinal < 0
      myPlatform = noone;
}

if myPlatform
   y += myPlatform.bbox_top - bbox_bottom;  //Snap the player to the top of the platform
Try that.
 
Last edited:

TheOnlyWRT

Member
Alright, i tried the code and the player still falls through the platform. Heres the code I used:
Code:
//check for a jump through platform
if (myPlatform == noone){ //If you're not already on a platform
   var jumpThrough = instance_place(x, y + vspFinal, objectJumpThrough);
   if(jumpThrough){ //this is a jumpthrough platform
 
      if((vspFinal >= 0) && (!place_meeting(x, y, jumpThrough))){ //we are moving down and should collide
      
          while(!place_meeting(x, y+sign(vspFinal), objectJumpThrough)){
    
            y = y + sign(vspFinal);
    
        }
        vspFinal = 0;
        vsp = 0;

        myPlatform = jumpThrough;
      }
   }
} else {
   //Set the platform to noone if the player is no longer colliding with it
   if(bbox_right < myPlatform.bbox_left || bbox_left > myPlatform.bbox_right || vspFinal < 0){
  
      myPlatform = noone;
      
     }
}

if(myPlatform){
   y += myPlatform.bbox_top - bbox_bottom;  //Snap the player to the top of the platform
}
 

TheouAegis

Member
Did you put my code in either the Step or End Step event of the player? You do not want a collision code. This line:
Code:
   var jumpThrough = instance_place(x, y + vspFinal, objectJumpThrough);
is in and of itself a collision code.So there's no point in using the collision event.

I think I need to make an edit to the code, but I gotta get to work. Still, you don't need a Collision Event, just the normal Step or End Step events.
 

TheOnlyWRT

Member
Did you put my code in either the Step or End Step event of the player? You do not want a collision code. This line:
Code:
   var jumpThrough = instance_place(x, y + vspFinal, objectJumpThrough);
is in and of itself a collision code.So there's no point in using the collision event.

I think I need to make an edit to the code, but I gotta get to work. Still, you don't need a Collision Event, just the normal Step or End Step events.
The code you gave me is in the step event, and I still haven't gotten it working either. Any other suggestions??
 

TheouAegis

Member
Does the plaatform object have a sprite_index or mask_index assigned to it?

Code:
//check for a jump through platform
if (myPlatform == noone){ //If you're not already on a platform
   var jumpThrough = instance_place(x, y + vspFinal, objectJumpThrough);
   if(jumpThrough){ //this is a jumpthrough platform

     if((vspFinal >= 0) && (!place_meeting(x, y, jumpThrough))){ //we are moving down and should collide
 
       vspFinal = 0;
       vsp = 0;
       myPlatform = jumpThrough;
     }
   }
} else {
   //Set the platform to noone if the player is no longer colliding with it
   if(bbox_right < myPlatform.bbox_left || bbox_left > myPlatform.bbox_right || vspFinal < 0){
 
     myPlatform = noone;
     
    }
}

if(myPlatform){
   y += myPlatform.bbox_top - bbox_bottom;  //Snap the player to the top of the platform
}
Try this.

Also make sure vspfinal is NOT being updated by this platform! If the platform is moving up, then it would make vspfinal negative, right? That would tell the game you are jumping, which breaks the bond between the platform and player.
 

TheOnlyWRT

Member
So that isnt quite working either, the player just kinda slowly falls through the platform and once he falls through, he still keeps falling at the same speed....
 

Gamebot

Member
What Thou Aegis posted DOES work perfectly! You are the master and my hero! Thank you for explaining why vertical platforms don't work with "traditional' code. Though they work fine with physics. That's another story.

Here is the full code for just the vertical part.

In obj_player ADD:

myPlatform = noone;
jumpThrough = false;

This is the FULL code for the vertical part in player step.

//Vertical Collision
if (place_meeting(x,y + vsp_final,obj_wall))
{
while(!place_meeting(x,y+sign(vsp_final),obj_wall))
{
y += sign(vsp_final);
}
vsp_final = 0;
vsp = 0;
}
y += vsp_final;

//check for a jump through platform
if (myPlatform == noone){ //If you're not already on a platform
var jumpThrough = instance_place(x, y + vsp_final, obj_platform_v);
if(jumpThrough){ //this is a jumpthrough platform

if((vsp_final >= 0) && (!place_meeting(x, y, jumpThrough))){ //we are moving down and should collide

vsp_final = 0;
vsp = 0;
myPlatform = jumpThrough;
}
}
} else {
//Set the platform to noone if the player is no longer colliding with it
if(bbox_right < myPlatform.bbox_left || bbox_left > myPlatform.bbox_right || vsp_final < 0){

myPlatform = noone;

}
}

if(myPlatform){
y += myPlatform.bbox_top - bbox_bottom; //Snap the player to the top of the platform
}

Obj_platform_v:

dir = -1;
movespeed = 3;
vsp = 0;

vsp = dir * movespeed;

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

dir *= -1;
}
y += vsp;

Thats it! Make sure your jumping from below and/or high enough on top. Also obviously change your code to suit as obj_wall in mine is the collision parent object.

P.S.: I forgot to mention my mask is same as sprite. Which is invisible!
 
Last edited:
Top