Skip to content

Instantly share code, notes, and snippets.

@bitlather
Last active April 28, 2020 06:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitlather/ca6f8481e29153b17aaf2c186f68858c to your computer and use it in GitHub Desktop.
Save bitlather/ca6f8481e29153b17aaf2c186f68858c to your computer and use it in GitHub Desktop.

For my comment on https://nikles.it/2020/gamemaker-tutorial/better-moving-platforms/

Note that I have my own scripts for creating/destroying data structures, which help me track leaks. create_list() and unregister_and_destroy_list() won't work for you.

Additionally, I did not include the logic move_platform_y_downwards() and move_platform_y_upwards() because it's a light refactoring of what can be found in the tutorial.

move_platform_y():

/// @param yvel
var _yvel = argument0;
var _ydir = sign(_yvel);

//
// Create lists.
//
var colliding_instance_list = create_list("move_platform_y().colliding_instance_list");
var instances_that_have_moved_with_previous_xy_list = create_list("move_platform_y().instances_that_have_moved_with_previous_xy_list");

//
// Track if we hit a solid while moving.
//
var has_hit_solid = false;

//
// Move one pixel at a time.
//
repeat abs(_yvel) {

    //
    // Collided with solid.
    //
    if is_colliding_y(_ydir) {

        has_hit_solid = true;
        break;

    }

    if _ydir > 0 {

        if !move_platform_y_downwards(
            colliding_instance_list                         ,
            instances_that_have_moved_with_previous_xy_list ) {

            has_hit_solid = true;
            break;

        }

    } else if _ydir < 0 {

        if !move_platform_y_upwards(
            colliding_instance_list                         ,
            instances_that_have_moved_with_previous_xy_list ) {

            has_hit_solid = true;
            break;

        }

    }

    //
    // Move the platform.
    //
    y += _ydir;
}

//
// Destroy data structures.
//
unregister_and_destroy_list(colliding_instance_list);
unregister_and_destroy_list(instances_that_have_moved_with_previous_xy_list);

//
// Return.
//
return !has_hit_solid;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment