EDIT 2: Pastebin link to the full ball, bat, and AIbat classes.
EDIT: I've updated the turbo code, and just commented it a bit better. I also have a link to all of my GameplayScreen code in Pastebin, as I didn't want to flood this page with it.
I'm making a pong type game. I'm creating a powerup which causes three things to occur:
1) Ball slows down (working) 2) screen turns black and white (working) 3 Bats slow down (not working)
I simply adjust the speed of the ball using the "speed" variable, and all works fine. I'm trying to do the same with the bats, but it is showing no change in the speed during the game. When I debug however, after the powerup is activated, I can hover over the speed variable and see that it has changed from it's default speed of 7.0f to 30.0f, but no visible change occurs within the game itself.
I'm also running a debug on screen which displays the speeds of the bats and ball, and I've noticed that the ball's speed changes accordingly, and he right bat (AiBat) does as well. It moves to 30.
For some reason though, the left bat (the player controlled one) remains the same speed. Odd.
I'm doing something wrong here, but I can't figure it out for the life of me. Why can I change speed in one location and it works fine, but not in another?
Class bat
{
/// <summary>
/// Controls the bat moving up the screen
/// </summary>
public void MoveUp()
{
SetPosition(Position + new Vector2(0, -moveSpeed * elapsedTime));
}
/// <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;
// Just here for debugging. Hitting Z WORKS FINE and slows the bats down
previous = current;
current = Keyboard.GetState();
if (current.IsKeyDown(Keys.Z))
{
elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds * 5.0f;
}
}
}
Class GameplayScreen
{
.......
private int disableCooldown;
private int coolDown;
private int powerDisableCooldown = 2000;
private int powerEnableCooldown = 5000;
......
public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
// Updating bat position
leftBat.UpdatePosition(ball, gameTime);
rightBat.UpdatePosition(ball, gameTime);
if (gScaleActivated == true)
{
leftBat.moveSpeed = 30.0f;
rightBat.moveSpeed = 30.0f;
}
// If spaceBar is down and the turbo bar is not empty, activate turbo. If not, turbo remains off
if (input.ActivateTurbo(ControllingPlayer))
{
if (disableCooldown > 0)
{
leftBat.isTurbo = true;
coolDown = powerEnableCooldown;
leftBat.moveSpeed = 30.0f;
disableCooldown -= gameTime.ElapsedGameTime.Milliseconds;
}
else
{
leftBat.DisableTurbo();
}
}
// If spacebar is not down, begin to refill the turbo bar
else
{
leftBat.DisableTurbo();
coolDown -= gameTime.ElapsedGameTime.Milliseconds;
// If the coolDown timer is not in effect, then the bat can use Turbo again
if (coolDown < 0)
{
disableCooldown = powerDisableCooldown;
}
}
// Makes sure that if Turbo is on, it is killd it after () seconds
if (leftBat.isTurbo)
{
disableCooldown -= gameTime.ElapsedGameTime.Milliseconds;
}
/// <summary>
/// Logic to trigger Powerups
/// </summary>
private void PowerupActivated(object sender, PowerupEventArgs e)
{
...........
case PowerupType.SlowMo:
{
this.SlowMo(gScaleActivated);
gScaleActivated = true;
break;
}
...........
}
// Activates the SlowMo and grayscale effect for the powerup.
public void SlowMo(bool gScaleActivated)
{
gScaleActivated = true;
ball.SlowMoBall();
AudioManager.Instance.PlaySoundEffect("SlowMotion1");
}
}