尝试比较两个坐标以获得结果时出错

时间:2022-08-11 21:21:57

I'm making a score counter for my game. When two object's x coordinates' meet the score increases. I wrote a little script and execute it from my manager object. The error then occurs from my script and the error response is nonsensical to me.

我正在为我的比赛制作一个得分计数器。当两个物体的x坐标'满足分数增加时。我写了一个小脚本并从我的经理对象中执行它。然后从我的脚本发生错误,错误响应对我来说是荒谬的。

The script:

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

The error is as such:

错误是这样的:

############################################################################################
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();

2 个解决方案

#1


Use this to make sure both instances exist:

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

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

Of course, if you have multiple instances of obj_char, you'll need to keep track of them. You can do that in whatever code you use to set up the room. For instance:

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

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

Now, you can place player or enemy in the same locations you would obj_char. Like this:

现在,您可以将玩家或敌人置于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;
};

Hope that helps. Let me know if you have any questions.

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

#2


The obj_char or obj_ball dont exists when you run the code. Try:

运行代码时,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;
            }
        } 
    }
}

Now you can have 0 or many obj_char and 0 or many obj_ball and the score will increase whenever two of them meet.

现在你可以拥有0或者许多obj_char和0或者许多obj_ball,只要其中两个满足,分数就会增加。

#1


Use this to make sure both instances exist:

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

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

Of course, if you have multiple instances of obj_char, you'll need to keep track of them. You can do that in whatever code you use to set up the room. For instance:

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

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

Now, you can place player or enemy in the same locations you would obj_char. Like this:

现在,您可以将玩家或敌人置于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;
};

Hope that helps. Let me know if you have any questions.

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

#2


The obj_char or obj_ball dont exists when you run the code. Try:

运行代码时,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;
            }
        } 
    }
}

Now you can have 0 or many obj_char and 0 or many obj_ball and the score will increase whenever two of them meet.

现在你可以拥有0或者许多obj_char和0或者许多obj_ball,只要其中两个满足,分数就会增加。