Using elapsed time for SlowMo in XNA

Posted by Dave Voyles on Game Development See other posts from Game Development or by Dave Voyles
Published on 2012-06-24T14:31:57Z Indexed on 2012/06/24 15:25 UTC
Read the original article Hit count: 256

Filed under:
|

I'm trying to create a slow-mo effect in my pong game so that when a player is a button the paddles and ball will suddenly move at a far slower speed. I believe my understanding of the concepts of adjusting the timing in XNA are done, but I'm not sure of how to incorporate it into my design exactly.

The updates for my bats (paddles) are done in my Bat.cs class:

        /// Controls the bat moving up the screen
    /// </summary>
    public void MoveUp()
    {
        SetPosition(Position + new Vector2(0, -moveSpeed));
    }

    /// <summary>
    /// Controls the bat moving down the screen
    /// </summary>
    public void MoveDown()
    {
        SetPosition(Position + new Vector2(0, moveSpeed));
    }

    /// <summary>
    /// Updates the position of the AI bat, in order to track the ball
    /// </summary>
    /// <param name="ball"></param>
    public virtual void UpdatePosition(Ball ball)
    {
        size.X = (int)Position.X;
        size.Y = (int)Position.Y;
    }

While the rest of my game updates are done in my GameplayScreen.cs class (I'm using the XNA game state management sample)

Class GameplayScreen
{
        ...........
        bool slow;
        ..........
      public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen) 

  base.Update(gameTime, otherScreenHasFocus, false);

     if (IsActive)
            {
                // SlowMo Stuff
                Elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
                if (Slowmo) Elapsed *= .8f;

                MoveTimer += Elapsed;

                    double elapsedTime = gameTime.ElapsedGameTime.TotalMilliseconds;

                if (Keyboard.GetState().IsKeyDown(Keys.Up))
                    slow = true;
                else if (Keyboard.GetState().IsKeyDown(Keys.Down))
                    slow = false;

                    if (slow == true)
                    elapsedTime *= .1f;

                    // Updating bat position
                leftBat.UpdatePosition(ball);
                rightBat.UpdatePosition(ball);

                // Updating the ball position
                ball.UpdatePosition();

and finally my fixed time step is declared in the constructor of my Game1.cs Class:

    /// <summary>
/// The main game constructor.
/// </summary>
public Game1()
{
    IsFixedTimeStep = slow = false;

}

So my question is: Where do I place the MoveTimer or elapsedTime, so that my bat will slow down accordingly?

© Game Development or respective owner

Related posts about XNA

Related posts about c#