1

我正在为我的游戏制作一个计分器。当两个对象的 x 坐标相遇时,分数增加。我写了一个小脚本并从我的管理器对象中执行它。然后从我的脚本中发生错误,错误响应对我来说是荒谬的。

剧本:

if (obj_char.x == obj_ball.x)
{
    obj_manager.myScore += 1;
}

错误是这样的:

############################################################################################
FATAL ERROR in
action number 1
of  Step Event0
for object obj_manager:

Push :: Execution Error - Variable Get 0.x(0, -2147483648)
 at gml_Script_scr_score (line 1) - if (obj_char.x == obj_ball.x)
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Script_scr_score (line 1)
called from - gml_Object_obj_manager_StepNormalEvent_1 (line 9) - scr_score();
4

2 回答 2

0

运行代码时 obj_char 或 obj_ball 不存在。尝试:

if instance_number(obj_char)>0 and instance_number(obj_ball)>0
{
    with obj_char
    {
        with obj_ball
        {
            if (other.x=x)
            {
                obj_manager.myScore+=1;
            }
        } 
    }
}

现在您可以拥有 0 个或多个 obj_char 和 0 个或多个 obj_ball,只要其中两个相遇,分数就会增加。

于 2015-11-17T08:25:04.633 回答
0

使用它来确保两个实例都存在:

if(instance_exists(obj_char) && instance_exists(obj_ball) && instance_exists(obj_manager)){
  if(obj_ball.x == obj_char.x) obj_manager.myScore += 1;
};

当然,如果您有多个 obj_char 实例,则需要跟踪它们。您可以使用用于设置房间的任何代码来执行此操作。例如:

player = instance_create(64, 64, obj_char);
enemy = instance_create(240, 64, obj_char);

现在,您可以将玩家或敌人放置在与 obj_char 相同的位置。像这样:

if(instance_exists(player) && instance_exists(obj_ball) && instance_exists(obj_manager)){
  if(obj_ball.x == player.x) obj_manager.myScore += 1;
};

if(instance_exists(enemy) && instance_exists(obj_ball) && instance_exists(obj_manager)){
  if(obj_ball.x == enemy.x) obj_manager.theirScore += 1;
};

希望有帮助。如果您有任何问题,请告诉我。

于 2015-09-13T00:53:04.633 回答