【问题标题】:XNA, Do-While basic troubleXNA, Do-While 基本问题
【发布时间】:2023-03-25 09:05:02
【问题描述】:

我正在为我的 XNA 项目创建一个简单的 AI。

我的敌人应该会在一段时间后从右向左移动。

不幸的是,我的代码跳过了我的第一个和第二个 Do-While,所以我的敌人没有移动:

while (i <= 1)
    {
        EnemyVelocity.X += 1f;
        timer += (float)gameTime.ElapsedGameTime.TotalSeconds;
        usedTimeRight = timer;
        i++;
    } 

    if (usedTimeRight != 0) 
    {
        do
        { EnemyVelocity.X -= 1f;
          timer += (float)gameTime.ElapsedGameTime.TotalSeconds;
        } while ((timer - usedTimeRight) >= 5);
        usedTimeLeft = timer;
        usedTimeRight = 0;
    }
    if (usedTimeLeft != 0)
    {
        do
        { EnemyVelocity.X += 1f;
          timer += (float)gameTime.ElapsedGameTime.TotalSeconds;
        }
        while (timer - usedTimeLeft >= 5);
        usedTimeRight = timer;
        usedTimeLeft= 0;
    }

更新了...~~~~~~~~~~~

所以,现在还有一个问题——我的敌人一直在向左移动

timer += (float)gameTime.ElapsedGameTime.TotalSeconds;

while (i <= 1)
    {
        EnemyVelocity.X -= 1f;
        timer += (float)gameTime.ElapsedGameTime.TotalSeconds;
        usedTimeRight = timer;
        i++;
    }

    if (usedTimeRight != 0 && (timer - usedTimeRight <= 2))
    { 
        int x;

        for (x = 0; x <= 3; x++)
        {
            EnemyVelocity.X = 1;

        }
        usedTimeLeft = timer;
        usedTimeRight = 0;
    }
  if (usedTimeLeft != 0 && (timer - usedTimeLeft <= 2))
    {
        int x;

        for (x = 0; x <= 3; x++)
        {
            EnemyVelocity.X = - 1;

        }
        usedTimeRight = timer;
        usedTimeLeft = 0;
    }

【问题讨论】:

    标签: c# xna artificial-intelligence game-physics


    【解决方案1】:

    问题在于初始 while 循环的迭代速度如此之快,以至于 while 循环在 usedTimeRight = timer > 0 之前退出。这意味着您的第一个和第二个 if 语句将是错误的,因为 useTimeLeft 和 @987654323 @ 将始终为 0。尝试更改此设置,使 Timer 变量在声明时等于 1。

    例如:

      float timer = 1f;
    
      while (i <= 1 ) 
                {
                    EnemyVelocity.X += 1f;
                    timer += (float)gameTime.ElapsedGameTime.TotalSeconds;
                    usedTimeRight = timer;
                    i++;
                } 
    
    
                if (usedTimeRight != 0) 
                {
                    do
                    { EnemyVelocity.X -= 1f;
                      timer += (float)gameTime.ElapsedGameTime.TotalSeconds;
                    } while ((timer - usedTimeRight) >= 5);
                    usedTimeLeft = timer;
                    usedTimeRight = 0;
                }
                if (usedTimeLeft != 0)
                {
                    do
                    { EnemyVelocity.X += 1f;
                      timer += (float)gameTime.ElapsedGameTime.TotalSeconds;
                    }
                    while (timer - usedTimeLeft >= 5);
                    usedTimeRight = timer;
                    usedTimeLeft= 0;
                }
    

    【讨论】:

    • 我已经稍微改变了我的代码(试图避免将计时器放在那个时候)但是我的角色现在只是向左移动..(如何添加新代码?^^)''
    • 我认为您需要通过查看每个点的所有值来思考代码
    【解决方案2】:

    我强烈建议初学者在视频游戏中使用 while 循环 仅在您真正需要时才更新循环,因为它可能会停止您的游戏或给您带来意想不到的结果(通常是在这个问题中传送的东西),因为一开始会遇到困难了解游戏内部逻辑是如何运作的。

    如果你需要你的敌人在五秒钟内做某事,只需在它开始你想要的动作时将 gameTime.ElapsedGameTime.totalMilliseconds 保存在一个变量中,然后检查你的 savedGameTime 是否小于总游戏时间 - 减去你想要的时间。

    如果做对了,你想让你的敌人向左移动五秒钟然后向右移动五秒钟?要做到这一点,你必须做这样的事情 :

            float delayBetweenMoves = 5f;
            float lastMoveTime = 0f;
            bool isMovingLeft;
            public void Update(GameTime gameTime)
            {
                Move(gameTime);
                enemy.Position += (enemy.VelocityX * enemy.Speed);
            }
            private void Move(GameTime gameTime)
            {
                if(lastMoveTime < gameTime.TotalGameTime.TotalMiliseconds - TimeSpan.FromSeconds(delayBetweenMoves))
                {
                    lastMoveTime = gameTime.TotalGameTime.TotalMiliseconds;
                    if (isMovingLeft)
                    {
                        enemy.VelocityX = -1;
                        isMovingLeft = false;
                    }
                    else
                    {
                        enemy.VelocityX = +1;
                    }
                }
            }
    

    【讨论】:

    • 我已经以与您建议的几乎相同的方式解决了我的问题 =)。谢谢你。顺便说一句,我注意到,退出 while 循环真的“很难”-> 所以我的程序只是冻结了:
    • 一揽子,绝对禁止whileUpdate 内循环不是一个好建议。没有内在的理由避免在那里使用while。确实,您不应该以他最初使用的方式使用while,完全禁止它只是教巫毒编程。
    • @BradleyUffner 我很惊讶你认为说初学者永远不要在视频游戏中使用潜在的无限循环是一个可怕的建议。在视频游戏中,您通常需要随着时间的推移尽可能快地做事,我几乎看不到任何情况下,while 循环最适合更新。但我完全承认我可能是错的,所以如果能启发我,我会很高兴^^
    • 一个立即浮现在脑海中的例子是走一棵树状结构。游戏实体通常放置在树形结构中,有时需要遍历父节点以查找特定属性。我对您的回答的主要问题是您使用绝对值,例如“从不”。如果你能改写其中的一些内容,我会删除我的反对意见。
    • 完成。我明白你的意思,我忘记了这个网站会随着时间的推移帮助人们,所以是的,我的答案是专门针对在这个特定时间提出这个问题的特定人,所以我假设他之前不会实施这种逻辑至少几个月,抱歉
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多