How to create a Wait () method in C # XNA?

Iโ€™m trying to add the โ€œattractionโ€ mode in my game, for this I need to tell the character to wait before moving (so that I can display the text on the screen). I decided that it would be advisable to create a Wait method in which I could send the following parameters.

    public bool Wait(int waitTime, GameTime gameTime)
    {
        // Wait Code
    }

The code will store the time it was called, wait for the specified time in milliseconds, and then return true when the time has passed.

However, I'm not sure how to make gameTime persist only the first time Wait (or bool never flags true) is called. I thought I could get internal bools to process the marks, but I'm not sure how I will program this in a dynamic / reusable way?

Any help is greatly appreciated and sorry for the essay! <3

+3
source share
3 answers

I solved a similar problem. Here is a snippet:

private static readonly TimeSpan intervalBetweenAttack1 = TimeSpan.FromMilliseconds(3000); 
private TimeSpan lastTimeAttack;

and inside Update () of the updated object

public override void Update(GameTime gameTime)
{
      // If enough time has passed attack
      if (lastTimeAttack + intervalBetweenAttack < gameTime.TotalGameTime)
      {
           Attack();
           lastTimeAttack = gameTime.TotalGameTime;
      }
}

This is a simplified AI code. He attacks every 3 seconds. If you want to attack only one, you can add bool hasAttacked = false; and then just just check it in Update ()

+2
source

You can create a timer class. Where you will do something like Timer t = new timer (3000f); // takes a value representing how long the timer works for

Triggers:

t.Start();

Update method:

if(!t.IsActive)
{
    //Player updates here
}
else
    t.Update(gameTime);

Waiting Method:

public void Wait()
{
    if(t.IsActive)//set by the .start method
    {
        if(t.isComplete)
        {
             //do some stuff
             t.Stop();
        }
        else
        {
            //we're still timing, nothing to do here
        }
    }
}

It is very rude and written from memory, so I'm not quite sure what will help you.

, , (- currentTime + = gameTime.elapsedgametime.milliseconds) , , IsComplete = true, , true .

, ,

0

Take a look at Thread.Sleep, MSDN

-2
source

All Articles