Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

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
You can’t perform that action at this time.