Search code examples
c#xnaartificial-intelligencegame-physics

XNA, Do-While basic trouble


I am creating a simple AI for my XNA project.

My Enemy should move from the right to the left after a short periode.

Unfortunately my code skips my first and second Do-While, so my enemy doesn't move :< 5 seconds long. So he just jumps from +-1.X Position

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

Updated...~~~~~~~~~~~

So, now another problem - my enemy is moving left all the time

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

Solution

  • The problem is with the fact that the initial while loop iterates faster so fast that the while loop is exited before usedTimeRight = timer > 0. This means that your fist and second if statments will be false because useTimeLeft and useTimeRight will always be 0. Try changing this so that the Timer variable is equal to 1 upon declaration.

    for example:

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