GameMaker 碰撞太快无法工作

GameMaker Collision Too fast to work

我在编程方面有点初学者,最近我一直在为 GameMaker Studio: Professional 中的一个学校项目编写游戏,并且 运行 遇到了一个小问题玩家降落在另一个敌人身上。因为现在我的玩家在足够高的向下速度下不会注册并摧毁敌人的实例,而只会受伤(或者游戏将重新开始,因为我还没有实现那部分)。已经测试了几件事,似乎玩家以更高的速度跳过了它可以摧毁敌人的范围,而是进入了敌人附近受到伤害,尽管在低速下玩家只是与敌人相撞美好的。玩家以可变高度跳跃,该高度由他们按住空格键的时间控制。我相信这可能是由于我的重力值,因为它不是整数但我不确定。我也试过给玩家增加一个终端速度,但是我试过的仍然保持相同游戏美感的数字并没有解决这个问题。

这是我的玩家对象和敌人对象的代码: 玩家对象:

Create Event:
execute code:

hsp = 0
vsp = 0
grav = 1.5
movespeed = 8
move = 0
jumpsmax = 2
jumps = 0
jumpspeed = 18
dir = 0

Step Event:

execute code:

//Movement
//Main controls
key_right = keyboard_check(ord("D"));
key_left = keyboard_check(ord("A")) * -1;
key_jump = keyboard_check_pressed(vk_space);
key_jump_held = keyboard_check(vk_space);

move = key_left + key_right;
hsp = move * movespeed;
vsp += grav;

if (place_meeting(x,y+1,obj_wall))
{
     jumps = jumpsmax;
}

if (keyboard_check(vk_down) != 1)
{
    if (key_jump) && (jumps > 0)
    {
       jumps -= 1;
       vsp = -jumpspeed;
    }
}

if (vsp < 0) && (!key_jump_held) vsp = max(vsp,-jumpspeed/3)

//Horizontal Collision
if (place_meeting(x+hsp,y,obj_wall))
{
    while(!place_meeting(x+sign(hsp),y,obj_wall))
    {
        x += sign(hsp);
    }
hsp = 0;
}
x += hsp;

//Vertical Collision
if (place_meeting(x,y+vsp,obj_wall))
{
    while(!place_meeting(x,y+sign(vsp),obj_wall))
    {
        y += sign(vsp);
    }
    vsp = 0;
}
y += vsp;
//Shooting
key_k = keyboard_check_pressed(ord("K"))
if (key_k > 0)
{
    bullet = instance_create(x+5*move,y,obj_pellet);
    bullet.hsp = image_xscale * 20;
}
//Animation
if (move!=0) image_xscale= move;
dir = image_xscale; 
if (place_meeting(x,y+1,obj_wall))
{
    if (move != 0)
    {
        sprite_index = spr_playerwalk;
    }
    else
    {
        sprite_index = spr_playeridle;
    }
}
if (vsp = -18) sprite_index = spr_playerjump;
if (sprite_index = spr_playerjump) && (image_index = 11) sprite_index = spr_playerrise;
if (vsp = 1.5) sprite_index = spr_playerfall;
if (sprite_index = spr_playerfall) && (image_index = 11) sprite_index = spr_playerdescend;
//Bullet Collision
if (place_meeting(x,y,obj_badpellet))
{
    with (obj_badpellet)
    {
        if (place_meeting(x +- 2,y,obj_player))
            {
                game_restart();
            }
    }
instance_destroy();
}



restart = keyboard_check(ord("R"))    
if (restart = 1) game_restart();

敌人对象:

execute code:

dir = 1;
hsp = 0;
vsp = 0;
grav = 0.8;
movespeed = 3;

Step Event:

execute code:

hsp = dir * movespeed;
vsp += grav;
image_xscale = dir * -1;
//Horizontal Collision
if (place_meeting(x+hsp,y,obj_wall))
{
    while(!place_meeting(x+sign(hsp),y,obj_wall))
    {
        x += sign(hsp);
    }
    hsp = 0;
    dir *= -1;
}
x += hsp;

//Vertical Collision
if (place_meeting(x,y+vsp,obj_wall))
{
    while(!place_meeting(x,y+sign(vsp),obj_wall))
    {
        y += sign(vsp);
    }
    vsp = 0;
}
y += vsp;

//Enemy Collison
if (place_meeting(x,y,obj_player))
{
    if (obj_player.y < y-16)
    {
        with (obj_player) vsp = -jumpspeed;
        instance_destroy();
    }
    else
    {
        game_restart();
    }
}
if (place_meeting(x,y,obj_pellet))
{
    with (obj_pellet)
    {
        if (place_meeting(x,y,obj_enemy))
            {
                instance_destroy();
            }
    }
instance_destroy();
}
if (place_meeting(x,y,obj_badpellet))
{
    with (obj_badpellet)
    {
        if (place_meeting(x,y,obj_enemy))
            {
                instance_destroy();
            }
    }
instance_destroy();
}

看起来你的问题是,当玩家击中敌人时,它太深入敌人而无法注册为击中它。

很难给您一个涵盖所有可能情况的答案,但一个简单的解决方法是:

if (obj_player.vsp > 0 and obj_player.y - obj_player.vsp < y-16)
{
    with (obj_player) vsp = -jumpspeed;
    instance_destroy();
}

我在这里做的是检查玩家是否正在坠落,然后检查玩家是否在上一步(即碰撞发生之前)在敌人上方。这将区分从上面落到敌人身上和从侧面进来。