0

我正在使用 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;')

4

1 回答 1

1

我注意到的一件事是您正在使用other内部 a with(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.
调用otherinside awith(other)可能会将目标指向未定义life变量的火球。这就是你的错误的来源。

于 2021-11-08T07:50:13.827 回答