当与另一个对象碰撞时,引用的变量是未定义的,即使它已定义?(GMS1.4)



我使用的是GameMaker Studio 1.4。我有一个火球物体,当它与敌人物体碰撞时,它应该从敌人的生命变量中移除1(一(。

这是代码:

火球代码

步骤事件

if (place_meeting(x,y,obj_enemy)) { // if collision with enemy
with (other) {
other.life-=1; // remove 1 from life
self.start_decay=true; // remove fireball
}
}

敌人代码

创建

life=1;
isDie=false;

步骤事件

if (life<=0) {
isDie=true; // I use a variable because there are other conditions that can also satisfy this
}

[...] // other unnecessary code

if (isDie) {
instance_destroy(); // Destroy self
}

错误日志

___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of  Step Event0
for object obj_fireball:
Variable <unknown_object>.<unknown variable>(100017, -2147483648) not set before reading it.
at gml_Object_obj_fireball_StepNormalEvent_1 (line 3) -         other.life-=1;
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_fireball_StepNormalEvent_1 (line 3) ('other.life-=1;')

我注意到的一件事是,您在with(other)中使用了other,这听起来有点不奇怪。

假设other.life -= 1是给敌人的,self.start_decay=true是给火球的,那么你可以去掉with(other)行(和括号(,保持这样的代码:

if (place_meeting(x,y,obj_enemy)) { // if collision with enemy
other.life-=1; // remove 1 from life
self.start_decay=true; // remove fireball
}

如果使用with(other),那么with(other)中的所有内容都将指向与其碰撞的"另一个"对象,在本例中为obj_enemy
with(other)中调用other可能会指向火球,而火球没有定义life变量。这就是你的错误来源。

最新更新