0

我在我的 GameMaker 游戏中做了一个简单的斜坡运动。

一切正常,除非玩家上坡并且它的速度足够高,没有时间对卡在墙上的碰撞代码做出反应。

这是它的外观:GIF

所以我的代码如下所示:

创建事件

hspd = 0;
vspd = 0;

grav = 2;

步骤事件

// Movement
if (keyboard_check(vk_right) && !keyboard_check(vk_left)) {
    hspd = 9;
} else
if (keyboard_check(vk_left) && !keyboard_check(vk_right)) {
    hspd = -9;
}

// Check not moving
if ((!keyboard_check(vk_right) && !keyboard_check(vk_left)) || (keyboard_check(vk_right) && keyboard_check(vk_left))) {
    hspd = 0;
}

// Gravity
if (!place_meeting(x, y+1, parent_ground)) {
    vspd += grav;
}

// Jumping
if (place_meeting(x, y+1, parent_ground)) {
    if (keyboard_check_pressed(vk_up)) {
        vspd = -20;
    }
}

// Variables
var hspd_final = hspd;

// Horizontal collision
horizontal_collision = instance_place(x+hspd_final, y, parent_ground);
if (horizontal_collision != noone) {
    if (horizontal_collision.object_index == object_ground) {
        while (!place_meeting(x+sign(hspd_final), y, horizontal_collision)) {
            x += sign(hspd_final);
        }
        hspd_final = 0;
    } else {
        // Declare yplus
        yplus = 0;

        // Loop
        while (place_meeting(x+hspd_final, y-yplus, parent_ground) && yplus <= abs(1*hspd_final)) {
            yplus += 1;
        }
        y -= yplus;
        while (place_meeting(x, y+1, parent_ground)) {
            y -= 1;
        }
    }
}

// Down slope
down_slope = instance_place(x, y+1, parent_ground);
if (down_slope != noone && down_slope.object_index != object_ground) {
    if (place_meeting(x, y+1, parent_ground)) {
        yminus = abs(hspd_final);

        while (place_meeting(x+hspd_final, y+yminus, parent_ground) && yminus != 0) {
            yminus --;
        }

        y += yminus;
    }
}

// Initialize horizontal speed
x += hspd_final;

// Vertical collision
if (place_meeting(x, y+vspd, parent_ground)) {
    while (!place_meeting(x, y+sign(vspd), parent_ground)) {
        y += sign(vspd);
    }
    vspd = 0;
}

// Initialize vertical speed
y += vspd;

我有 3 个墙壁对象:object_ground(基本块)、object_slope(精确坡度对象)和parent_ground(子对象是 object_ground 和 object_slope 的对象)。

谢谢你。

4

2 回答 2

2

一个简单而适当的解决方案可以很好地解决这个问题,就是检查两者之间的位置。不要spd一次移动它像素,而是移动它 1 像素spd次。如果同时您遇到碰撞,您已经达到最大值。

这样,无论您以0.01速度还是以9999.0速度移动都无关紧要。

于 2019-06-04T14:08:37.497 回答
0

这是游戏制作者很常见的问题,它只有两个真正的解决方案。

  1. 改变你的房间速度,这样它就可以跟上
  2. 降低播放器(白色立方体)的速度
于 2019-05-30T15:41:01.130 回答