问题:我的所有/大多数Powerup都是立即激活,而不是仅存储随机选择(滚动)的1,然后按下LeftCtrl键激活。
我仍然不习惯使用枚举,而且在使用EventArgs
时非常新手。
我已经读了很多,但仍然不确定它们是如何工作的,我相信这就是我的问题所在。
除了Powerup类之外,我还在下面的Game1类中添加了代码。
课程: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);
}
课程:通电
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);
}
}
}
答案 0 :(得分: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.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);
}
可能还有更多,但这是我到目前为止看到的。如果你想在某个地方上传项目并给我发电子邮件链接,那很好。