D
Docker
Guest
GM Version: Studio
Target Platform: Windows
Download: see code below
Links: N/A
HORIZONTAL PLATFORM:
Target Platform: Windows
Download: see code below
Links: N/A
HORIZONTAL PLATFORM:
I created this while doing Shaun Spalding's platformer series as I wanted to use states and my own implementations based on the theory of his videos.
This guide assumes you have created your own vspd and hspd variables to use for movement, if you followed Shauns guide then change any vspd and hspd reference to vsp and hsp instead.
There are probably better ways of doing this but it seemed like a logical fully functioning implementation so I thought i'd share it as I struggled to find anything similar when I tried looking for help while creating it.
1. Create a sprite called spr_moving_platform_h sprite and an object called obj_moving_platform_h, add the sprite to the object.
Add a create event in the object containing:
All this code will do is set your object's state to one of the above which we will be creating, the state will control the movement and basic collision detection
Add a step event containing:
Pretty self explanatory.
2. Create a script called platform_left_state and another script called platform_right_state.
Inside the platform_left_state put:
Inside the platform_right_state put:
Then create a collision event inside the player object with the obj_moving_platform_h and add:
the variables left_platform_side and right_platform_side are assuming that your platform sprite is 32 pixels wide, if you have used a different width just replace 16 with the width of your sprite/2, I'm sure there is also a command you can use to get sprite width/2 that is probably better practice than using an absolute value.
For this to work you will need the variable hspd_carry = 0; to be initialised in the player objects create event, you will need to update Solid to whatever you have named your wall object or parent object as and in your normal collision event code you will need:
Any reference to hspd inside the collision event checking statements need to be changed to hspd_final and where you reset hspd = 0 you also need to do hspd_final = 0 as well as update your x+= from hspd to hspd_final.
This creates a solid platform that the player is fully capable of jumping and standing on, if the player jumps into the side it'll act like a wall and push them away to stop you getting stuck in it unless there is a wall between you and the platform in which case it will change direction.
You could make this kill the player instead if you want but I chose to do it this way for simplicity.
This guide assumes you have created your own vspd and hspd variables to use for movement, if you followed Shauns guide then change any vspd and hspd reference to vsp and hsp instead.
There are probably better ways of doing this but it seemed like a logical fully functioning implementation so I thought i'd share it as I struggled to find anything similar when I tried looking for help while creating it.
1. Create a sprite called spr_moving_platform_h sprite and an object called obj_moving_platform_h, add the sprite to the object.
Add a create event in the object containing:
Code:
/// Initialise platform
state = choose(platform_left_state, platform_right_state);
Add a step event containing:
Code:
///Execute state
script_execute(state);
2. Create a script called platform_left_state and another script called platform_right_state.
Inside the platform_left_state put:
Code:
///platform_left_state
var wall_at_left = place_meeting(x-3, y, Solid); // Check for a wall 3 pixels left of platform
if (wall_at_left){
while (!place_meeting(x - 1, y, Solid)){ // while we are not 1 pixel away, move 1 pixel closer
x -= 1;
}
if (place_meeting(x, y - 1, obj_player)){
obj_player.hspd_carry = 3; // if a player is on platform, add 3 to their hspd, stops player moving 3 pixels on direction change
}
state = platform_right_state; // change state to right as we are 1 pixel from wall
} else {
if (place_meeting(x, y - 1, obj_player)){
obj_player.hspd_carry = -3; // if player on platform, add -3 to hspd to move them left
}
x -= 3; // move platform 3 pixels left every step
}
Code:
///platform_right_state
var wall_at_right = place_meeting(x+3,y,Solid); // Check for a wall 3 pixels left of platform
if (wall_at_right){
while (!place_meeting(x + 1, y, Solid)){ // while we are not 1 pixel away from wall, move 1 pixel closer
x += 1;
}
if (place_meeting(x, y - 1, obj_player)){
obj_player.hspd_carry = -3; // if a player is on platform, add 3 to their hspd, stops player moving 3 pixels on direction change
}
state = platform_left_state;
} else {
if (place_meeting(x, y - 1, obj_player)){
obj_player.hspd_carry = 3; // if player on platform, add 3 to hspd to move them right
}
x += 3; // move platform 3 pixels right every step
}
Code:
/// Collision implementation
var left_platform_side = x < other.x-16; // if my x position is 1 pixel left of platform
var right_platform_side = x > other.x+16; // if my x position is 1 pixel right of platform
if (left_platform_side){
if (place_meeting(x-4, y, Solid)){ // if wall 4 pixels to the left of player
with (other){
state = platform_right_state; // change platform direction
}
} else {
x -= 4; // push me away from platform by 4 pixels
}
}
if (right_platform_side){
if (place_meeting(x+4, y, Solid)){ // if wall 4 pixels to the right of player
with (other){
state = platform_left_state; // change platform direction
}
} else {
x += 4; // push me away from platform by 4 pixels
}
}
For this to work you will need the variable hspd_carry = 0; to be initialised in the player objects create event, you will need to update Solid to whatever you have named your wall object or parent object as and in your normal collision event code you will need:
Code:
var hspd_final = hspd + hspd_carry; // platform hspd plus player hspd
hspd_carry = 0;
This creates a solid platform that the player is fully capable of jumping and standing on, if the player jumps into the side it'll act like a wall and push them away to stop you getting stuck in it unless there is a wall between you and the platform in which case it will change direction.
You could make this kill the player instead if you want but I chose to do it this way for simplicity.
Last edited: