小编Ren*_*net的帖子

敌人随机移动方向

我正在自上而下的射击游戏中工作,我希望我的敌人漫无目的地游荡。在这段代码中,我有一个Enemy类,其中的所有变量(旋转,位置等)包括主类中的random(rnd)。

我的问题是敌人不断从屏幕的右上角移动到左下角。因此,他们不会四处走动,但始终会朝着一定的方向前进。我想知道为什么会这样。

refresh += gameTime.ElapsedGameTime.TotalSeconds;

if (refresh >= 3)
{
    refresh -= 3;
    int rndDirection = rnd.Next(4);

    if (rndDirection == 1) vektor = new Vector2(0, -1);
    if (rndDirection == 2) vektor = new Vector2(0, 1);
    if (rndDirection == 3) vektor = new Vector2(1, 0);
    if (rndDirection == 4) vektor = new Vector2(-1, 0);

    foreach (Enemy enemy in enemies)
    {
        Vector2 rndDirVec = vektor;
        enemy.directionX = enemy.position.X - rndDirVec.X;
        enemy.directionY = enemy.position.Y + rndDirVec.Y;
        enemy.rotation = (float)Math.Atan2(enemy.directionY, enemy.directionX);
    }
}
foreach …
Run Code Online (Sandbox Code Playgroud)

c# random xna direction

0
推荐指数
1
解决办法
4440
查看次数

标签 统计

c# ×1

direction ×1

random ×1

xna ×1