1
문제 : 무작위로 선택한 (굴림) 1을 저장하는 대신 내/모든 Powerups를 한 번에 모두 활성화 한 다음 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);
}
}
}
선택 취소 된 파워 업의 이벤트 처리기를 어떻게 취소 하시겠습니까? – Oded
죄송합니다. 아직 프로그래밍에 익숙하지 않습니다. 이벤트 처리기를 구독 취소하면 무엇을 의미합니까? –
이 코드 줄에서 :'powerup.Activated + = PowerupActivated;'PowerupActivated' 이벤트 핸들러를'Activated' 이벤트에 등록합니다 (그래서'powerup'에서'Activated'가 발생하면'PowerupActivated'가 호출됩니다). 이 구독을 _removing_하고있는 방법을 묻습니다. – Oded