You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GameMaker Studio中place_meeting碰撞判断异常:为何始终输出A?

问题描述

我创建了名为static_world_object的对象,将其作为所有无生命物体的父对象,并在其Step事件中添加代码,用于检测玩家对象是否与之发生碰撞。代码如下:

if self.image_alpha < 1{ // 检查静态物体是否变为透明

    var obj_player_instance;
    obj_player_instance = instance_find(obj_player,0);

    if !place_meeting(obj_player_instance.x, obj_player_instance.y, self){
        show_debug_message("A");}
    else{
        show_debug_message("B");
    }
}

无论是否与该无生命物体碰撞,调试信息始终输出“A”。请问问题出在哪里?


问题原因与解决办法

核心问题:place_meeting默认忽略当前实例

你调用place_meeting(obj_player_instance.x, obj_player_instance.y, self)时,self指代当前运行代码的static_world_object子实例,但place_meeting函数的逻辑是检测指定位置是否存在目标对象的实例,但会自动排除调用该函数的实例本身。所以不管玩家有没有碰到当前这个无生命物体,函数都会返回false,!false等价于true,自然一直输出"A"。

针对性解决办法

根据你的实际需求选择对应的方案:

1. 检测玩家是否碰到任意无生命物体(父对象的子实例)

如果你的需求是判断玩家是否和任何属于static_world_object的子实例碰撞,把place_meeting的第三个参数改为父对象static_world_object即可:

if self.image_alpha < 1{
    var obj_player_instance = instance_find(obj_player,0);
    if obj_player_instance == noone { return; } // 玩家不存在时直接退出,避免报错

    if !place_meeting(obj_player_instance.x, obj_player_instance.y, static_world_object){
        show_debug_message("A");
    } else {
        show_debug_message("B");
    }
}

2. 检测玩家是否碰到当前这个无生命物体实例

如果需要单独检测玩家是否碰到运行这段代码的具体实例,改用instance_position或collision_point函数(这两个支持检测实例自身):

if self.image_alpha < 1{
    var obj_player_instance = instance_find(obj_player,0);
    if obj_player_instance == noone { return; }

    // 使用instance_position的写法
    var _collision_instance = instance_position(obj_player_instance.x, obj_player_instance.y, self);
    if !_collision_instance {
        show_debug_message("A");
    } else {
        show_debug_message("B");
    }

    // 或者使用collision_point的写法(更灵活,可控制是否忽略自身)
    /*
    if !collision_point(obj_player_instance.x, obj_player_instance.y, self, false, true){
        show_debug_message("A");
    } else {
        show_debug_message("B");
    }
    */
}

额外提示

instance_find(obj_player,0)在玩家实例未创建或已销毁时会返回noone,提前判断可以避免后续代码出现异常。


内容的提问来源于stack exchange,提问作者nubemuffin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 00:34:50