2012-04-28 6 views
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); 
     } 
    } 
} 
+0

선택 취소 된 파워 업의 이벤트 처리기를 어떻게 취소 하시겠습니까? – Oded

+0

죄송합니다. 아직 프로그래밍에 익숙하지 않습니다. 이벤트 처리기를 구독 취소하면 무엇을 의미합니까? –

+0

이 코드 줄에서 :'powerup.Activated + = PowerupActivated;'PowerupActivated' 이벤트 핸들러를'Activated' 이벤트에 등록합니다 (그래서'powerup'에서'Activated'가 발생하면'PowerupActivated'가 호출됩니다). 이 구독을 _removing_하고있는 방법을 묻습니다. – Oded

답변

1

커플 :

당신이 파워 업의 일부를 활성화하는 것처럼 보이는
  • 즉시 는 HUD를 호출하여 , 공 및 박쥐 방법. 그럴까요? 플레이어가 수동으로 활성화해야한다고 생각했습니다. 이러한 호출은 파워 업의 Activate 이벤트에 의해 호출되는 이벤트 핸들러로 전달됩니다.
  • input.RollPowerup 및 input.ActivatePowerup을 어딘가에 다시 설정 하시겠습니까? 그렇지 않은 경우 예기치 않은 문제가 발생할 수 있습니다. 각 프레임마다 새로운 전원이 생성 될 수 있기 때문에 즉시 모든 전원 공급이 활성화 될 수 있습니다.
  • 게임 클래스의 생성자 또는 Initialize 메서드로 이벤트 연결을 이동하려고 할 수도 있습니다. 그러면 각 프레임이 진행되고 있다고 가정하고있는 검사를 제거 할 수 있습니다.
  • 매번 새로운 전원을 만들 필요는 없습니다. 선택한 유형으로 재설정하십시오. 이것은 IsActive와 같은 powerup 클래스에 속성을 추가하는 것을 의미합니다.

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); 
} 

이보다 더있을 수 있습니다,하지만 그건 내가 지금까지 볼 수있는 작업은 다음과 같습니다 코드는 다음과 같을 것이다. 프로젝트를 어딘가에 업로드하고 링크를 이메일로 보내려면 괜찮습니다.

관련 문제