Issue: All/most of my Powerups are activating at once, instead of just storing the 1 which has randomly been selected (rolled), and then activated by pressing the LeftCtrl key.
I'm still new to using enumerations, and am very much a novice when it comes to utilizing EventArgs
.
I've read up on them quite a bit, but still not exactly sure of how they work, and I believe that is where my issue may lie.
I've included code from my Game1 class below, in addition to the Powerup class.
Class: Game1
if (input.RollPowerup == true)
{
//generate a random powerup
PowerupType type = (PowerupType)random.Next(Enum.GetNames(typeof(PowerupType)).Length);
switch (type)
{
case PowerupType.BigBalls:
{
powerup = new Powerup(type, 10.0f, 1.0f);
ball.GrowBall();
break;
}
case PowerupType.ShrinkBalls:
{
powerup = new Powerup(type, 10.0f, 1.0f);
ball.ShrinkBall();
break;
}
case PowerupType.BigPaddle:
{
powerup = new Powerup(type, 10.0f, 1.0f);
leftBat.GrowBat();
break;
}
case PowerupType.ShrinkEnemy:
{
powerup = new Powerup(type, 10.0f, 1.0f);
rightBat.ShrinkBat();
break;
}
case PowerupType.SpeedBall:
{
powerup = new Powerup(type, 10.0f, 20.0f);
ball.IncreaseSpeed();
break;
}
case PowerupType.Heal:
{
powerup = new Powerup(type, 1.0f, 1.0f);
hud.AddHealthP1();
break;
}
case PowerupType.Regen:
{
powerup = new Powerup(type, 20.0f, 1.0f);
break;
}
}
powerupInitialized = false;
}
else if (input.ActivatePowerup == true)
{
powerup.Activate();
}
if (powerup != null && IsActive)
{
if (!powerupInitialized)
{
powerup.Activated += PowerupActivated;
powerup.Deactivated += PowerupDeactivated;
if (powerup.Type == PowerupType.Regen)
powerup.Tick += PowerupTick;
powerupInitialized = true;
}
powerup.Update((float)gameTime.ElapsedGameTime.TotalMilliseconds);
}
Class: Powerup
public void Activate()
{
IsAlive = true;
// powerupActivate.Play(); // Plays the SFX to notify user that item has been selected
if (Activated != null)
Activated(this, eventArgs);
if (Type <= PowerupType.ThreeBurst)
{
//no need to call Deactivated unless you really want to
IsAlive = false;
}
}
public virtual void Update(float elapsed)
{
// Checks if the player is still alive or not
if (IsAlive)
{
elapsedLifetime += elapsed;
if (elapsedLifetime >= maxLifetime)
{
IsAlive = false;
if (Deactivated != null)
Deactivated(this, eventArgs);
}
else if (elapsedLifetime >= (lastTick + 1) * 1000)
{
lastTick++;
if (Tick != null)
Tick(this, eventArgs);
}
}
}