问题:我的所有/大部分 Powerups 都立即激活,而不是仅存储随机选择(滚动)的 1,然后按 LeftCtrl 键激活。
我对使用枚举还是个新手,在使用EventArgs
.
我已经对它们进行了很多阅读,但仍然不确定它们是如何工作的,我相信这就是我的问题所在。
除了 Powerup 类之外,我还在下面的 Game1 类中包含了代码。
类别:游戏1
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);
}
类别:通电
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);
}
}
}