我想在 GameMaker 中创建一个单向平台,如何做?

问题描述 投票:0回答:1

所以我想为我的游戏创建一个单向平台,我是 GameMaker(以及一般编码)的初学者,也是 StackOverflow 的新手,所以我不知道如何去做。 我有我的 obj_player,带有创建和步骤事件。 我有一个 obj_oneway ,没有附加任何代码。

这是我的播放器的创建和步骤事件:

// Create Event
move_speed = 4; jump_speed = 16;
move_x = 0; move_y = 0;

tmWorld1 = layer_tilemap_get_id("tiles");
// Step Event
move_x = move_speed * sign(image_xscale); // Player will move in the direction it is facing

// Check if there is a wall in the direction the player is moving
if (place_meeting(x + move_x, y, tmWorld1)) {
    image_xscale *= -1; // Flip the sprite and change direction
}

// Perform a custom collision check with obj_oneway
var oneway_id = instance_place(x + move_x, y, obj_oneway);

if (oneway_id && oneway_id.object_index == obj_oneway) {
    // Check if the player is moving downward and is above the platform
    if (move_y > 0 && y <= oneway_id.y) {
        // Save the original collision mask
        var original_mask = oneway_id.mask_index;

        // Temporarily disable collisions from below
        oneway_id.mask_index = 0;

        // Apply movement
        move_and_collide(move_x, move_y, tmWorld1);

        // Restore the original collision mask
        oneway_id.mask_index = original_mask;
    }
    else {
        // If the player is not moving downward or is above, perform a normal collision check
        if (place_meeting(x + move_x, y, tmWorld1)) {
            // If there's a wall in front, stop horizontal movement
            move_x = 0;
        }
        move_and_collide(move_x, move_y, tmWorld1);
    }
}
else {
    // If the player is not on a one-way platform, perform a normal collision check
    if (place_meeting(x + move_x, y, tmWorld1)) {
        // If there's a wall in front, stop horizontal movement
        move_x = 0;
    }
    move_and_collide(move_x, move_y, tmWorld1);
}

// Check if there's ground below and handle jumping
if (place_meeting(x, y + 2, tmWorld1)) {
    move_y = 0;

    if (keyboard_check_pressed(vk_space)) { // Use keyboard_check_pressed to trigger jump only once
        move_y = -jump_speed;
    }
} else {
    if (move_y < 10) {
        move_y += 1;
    }
}

我制作了两个精灵的碰撞蒙版,为它们两个制作了一个矩形。 当我在平台下方时,我可以穿过它,但是当我落在平台上时,我也可以,并且我希望玩家在平台上行走。 在图块层上,有一些图块,但是单向平台不在这次碰撞上,它是一个对象,我将其保存在实例层中。 我已经尝试了很多东西,但我仍然不明白为什么它不起作用。 如果您对我的代码有建议,我也会采纳。 谢谢!

game-physics game-development game-maker gml
1个回答
0
投票

我认为你在这里同时进行了瓷砖碰撞和物体碰撞,尽管我只经历过物体碰撞。平台是一个对象,所以它可能会起作用。

根据我对平台体验的记忆,我让平台充当一个实体对象(一个你无法穿过的实体方块,你可以通过使平台成为该实体对象的子对象来做到这一点),然后使用按钮暂时忽略平台对象。 (与你所做的类似)我这边棘手的部分是平台图块比实心图块短得多,因此角色可能移动得足够快,以便在两次碰撞检查之间“阶段”通过平台。因此,您可能需要确保平台足够厚,以免您无法以太快的速度通过它。

© www.soinside.com 2019 - 2024. All rights reserved.