XNA, Do-While基本问题

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

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

不幸的是,我的代码跳过了我的第一个和第二个Do-While循环,所以我的敌人没有移动:< 5秒钟。所以他只是从+-1.X位置跳跃

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;    }

回答:

问题在于初始的while循环迭代速度太快,以至于在usedTimeRight = timer > 0之前就退出了while循环。这意味着你的第一个和第二个if语句将为假,因为useTimeLeftuseTimeRight总是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;            }

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注