GameMaker最新版本中'unassigned variable instance_create referenced'错误如何解决?
问题分析与解决
错误原因
最新版GameMaker已移除instance_create这个旧函数,该函数仅在GameMaker Studio 2.3之前的版本中可用。你的代码调用了这个不存在的函数,因此触发"unassigned variable instance_create referenced"错误。所谓"缺少扩展"的说法只适用于极早期旧版本,当前版本完全不适用。
修复方案
将代码中的instance_create替换为当前版本支持的instance_create_layer(按图层创建实例,推荐)或instance_create_depth(按深度创建实例)。
修改后的代码
// Initialize destination coordinates if they don't exist if (is_undefined(_destinationX) || is_undefined(_destinationY)) { // 使用instance_create_layer,参数依次为x坐标、y坐标、图层名称、目标对象 _destinationX = instance_create_layer(0, 0, "Instances", obj_null); show_message("Instance created for _destinationX"); _destinationY = instance_create_layer(0, 0, "Instances", obj_null); show_message("Instance created for _destinationY"); } // Set the coordinates for _destinationX and _destinationY objects if ((!_destinationX.x && _destinationX.x != 0) || (!_destinationY.y && _destinationY.y != 0)) { _destinationX.x = x + 64; // set x coordinate _destinationX.y = y; // set y coordinate _destinationY.x = x; // set x coordinate _destinationY.y = y + 64; // set y coordinate } // Move towards the destination move_towards_point(_destinationX.x, _destinationY.y, _speed);
关键注意点
- 第三个参数
"Instances"是项目中实际存在的图层名称,若你的项目图层命名不同,需对应修改为实际名称。 - 若偏好按深度控制实例,可改用
instance_create_depth(0, 0, 0, obj_null),最后一个参数为深度值,按需调整。
额外优化建议
你用空实例存储坐标的方式完全没必要,直接用普通变量存储数值即可,能减少不必要的实例创建,提升性能:
// Initialize destination coordinates if they don't exist if (is_undefined(_destinationX) || is_undefined(_destinationY)) { _destinationX = x + 64; _destinationY = y + 64; } // Move towards the destination move_towards_point(_destinationX, _destinationY, _speed);
内容的提问来源于stack exchange,提问作者Seiso
相关产品推荐
相关产品推荐

