UPDATE 2: I've tried to pass in GameTime to my Update method, just as I did for the bats, but I'm not seeing any change to the ball's speed. I've also changed my moveSpeed to:
*moveSpeed += (15f * elapsedTime);*
where previously it was simply moveSpeed += 15f; and it still hasn't yielded any results. What could I be doing wrong?
Class Ball
{
public void UpdatePosition(GameTime gameTime)
{
elapsedTime = 50.0f * (float)gameTime.ElapsedGameTime.TotalSeconds;
size.X = (int)position.X;
size.Y = (int)position.Y;
oldPos.X = position.X;
oldPos.Y = position.Y;
position.X += speed * ((float)Math.Cos(direction));
position.Y += speed * ((float)Math.Sin(direction));
bool collided = CheckWallHit();
particleEngine.Update();
previous = current;
current = Keyboard.GetState();
if (current.IsKeyDown(Keys.Z))
{
elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds * 5.0f;
}
/// <summary>
/// Check for the ball to return normal speed after the Powerup has expired
/// </summary>
public void NormalSpeed(GameTime gameTime)
{
moveSpeed += (15f * elapsedTime);
}
}
UPDATE:
I seem to have found a temporary solution, by using the follow method, however I am not sure of where I should place ElapsedTime so that it affects ALL of my classes and their updates (ex: the ball as well). As it stands, when I hit Z the bat will slow down because I am changing the ElapsedTime float, but I think I may have figured out a way to allow the ball to use it too. I may just have the Z key turn float down from 50 into 5 in all classes when pressed:
/// <summary>
/// Updates the position of the AI bat, in order to track the ball
/// </summary>
public virtual void UpdatePosition(Ball ball, GameTime gameTime)
{
size.X = (int)Position.X;
size.Y = (int)Position.Y;
elapsedTime = 50.0f * (float)gameTime.ElapsedGameTime.TotalSeconds;
previous = current;
current = Keyboard.GetState();
if (current.IsKeyDown(Keys.Z))
{
elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds * 5.0f;
}
}
/// <summary>
/// Controls the bat moving up the screen
/// </summary>
public void MoveUp()
{
SetPosition(Position + new Vector2(0, -moveSpeed * elapsedTime));
}
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?