1

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);
        }
    }
}
Oded
  • 489,969
  • 99
  • 883
  • 1,009
Dave Voyles
  • 4,495
  • 7
  • 33
  • 44
  • How are you unsubscribing the event handlers for powerups that are de-selected? – Oded Apr 28 '12 at 19:58
  • I'm sorry, I'm still very new to programming. What do you mean by unsubscribing the event handlers? – Dave Voyles Apr 28 '12 at 23:19
  • In this line of code: `powerup.Activated += PowerupActivated;` you are subscribing the `PowerupActivated` event handler to the `Activated` event (so, when `Activated` occurs on the `powerup`, `PowerupActivated` is invoked). I am asking you how you are _removing_ this subscription. – Oded Apr 29 '12 at 08:09
  • Oh ok, I understand what you are saying now. To tell you the truth, I am not quite sure. I suppose I would need a way to negate that. Now that I'm aware of what the issue is, let me make some changes and come back tomorrow to let you know how they worked out. Thank you! – Dave Voyles Apr 30 '12 at 00:26
  • I've had some time to go over this now, and I see what you mean. I wrongfully assumed that " powerup.Deactivated += PowerupDeactivated;" just beneath that would turn off the other powerups, but that doesn't seem to be the case. I guess I need to figure out how to unsubscribe from the other powerups, while still storing the 1 which has been randomly rolled. I'm not quite sure of how to go about that with events, however. – Dave Voyles May 01 '12 at 10:54
  • You can try unsubscribing _before_ assigning the new powerup (so just after the `if` statement, something like `powerup.Deactivated -= PowerupDeactivated;`). – Oded May 01 '12 at 11:01

1 Answers1

1

Couple of things:

  • It looks like you're activating some of the powerups immediately by calling hud, ball, and bat methods. Is that the case? I thought the player had to activate them manually? Those calls would go in the event handler that's called by the powerup's Activate event.
  • Are you resetting input.RollPowerup and input.ActivatePowerup to false somewhere? If not, that could be causing unexpected problems, possibly all the powerups activating at once since a new powerup would be generated each frame.
  • you also probably want to move the hooking up of the events to the game class's constructor or Initialize method. That would eliminate the check, which I'm assuming is happening each frame.
  • You probably don't need to create a new powerup each time. Just reset it to the type that's selected. This would mean adding a property to the powerup class - something like IsActive. Your code would then look like:

if (input.RollPowerup == true)
{
    //generate a random powerup
    PowerupType type = (PowerupType)random.Next(Enum.GetNames(typeof(PowerupType)).Length);
   switch (type)
    {
        case PowerupType.BigBalls:
            {
                powerup.Reset(type, 10.0f, 1.0f);

                break;
            }

         case PowerupType.ShrinkBalls:
            {
                powerup.Reset(type, 10.0f, 1.0f);

                break;
            }
        case PowerupType.BigPaddle:
            {
                powerup.Reset(type, 10.0f, 1.0f);

                break;
            }
        case PowerupType.ShrinkEnemy:
            {
                powerup.Reset(type, 10.0f, 1.0f);

                break;

            }
        case PowerupType.SpeedBall:
            {
                powerup.Reset(type, 10.0f, 20.0f);

               break;

            }
        case PowerupType.Heal:
            {
                powerup.Reset(type, 1.0f, 1.0f);

                break;
            }
        case PowerupType.Regen:
            {
                powerup.Reset(type, 20.0f, 1.0f);

                break;
            }
    }

    powerupInitialized = false;

}
else if (input.ActivatePowerup == true)

{
    powerup.Activate();
}

if (powerup.IsActive)
{
    powerup.Update((float)gameTime.ElapsedGameTime.TotalMilliseconds);
}

There might be more to it than this, but that's what I see so far. If you want to upload the project somewhere and email me the link, that's fine.

Jim Perry
  • 435
  • 1
  • 3
  • 13