2013-06-04 36 views
-1

基本上整天使用Forms应用程序后,我创建了一个简单的UI和一些keydown事件。大!但现在,我去运行它,它突然开始抛出一个System.TypeInitializationException。通常情况下,我可以自己找出这样的问题(使用Visual Studio的帮助),但是Visual Studio不会在编码时发现任何错误。突然发生System.TypeInitializationException,Visual Studio没有指出错误

这里是我的Form1.cs中:

public Form1() 
    { 
     Shown += new EventHandler(FormShow); 
     KeyDown += new KeyEventHandler(Form1_KeyDown); 
     InitializeComponent(); 
    } 

    // ------------------------------- // 
    // --------- Game Code ----------- // 
    // ------------------------------- // 

    public void FormShow(object sender, System.EventArgs e) 
    { 
     GameState = 0; 
     //Begin HUDRendererThread 
     StartHUDRenderThread(this); 
     GameState = 2; 
    } 

    // ------------------------------- // 
    // -- Methods Used By This File -- // 
    // ------------------------------- // 

    public void Form1_KeyDown(object sender, KeyEventArgs e) 
    { 
     if (e.KeyCode == Keys.H) 
     { 
      DamagePlayer(ThePlayer, 5); 
     } 
    } 

    public void DamagePlayer(Player playa, int damage) 
    { 
     playa.playerHP = playa.playerHP - damage; 
    } 

    // ------------------------------- // 
    // ----- HUD Renderer Thread ----- // 
    // ------------------------------- // 

    public class ThreadForRenderingHUD 
    { 
     public Form from; 

     public GameHUD initRenderHUD(Player playerUsed, string desiredName) 
     { 
      GameHUD newHUD = new GameHUD(); 
      newHUD.Name = desiredName; 
      newHUD.playerToGetStatisticsFrom = playerUsed; 
      return newHUD; 
     } 

     public void GetSettedHP(GameHUD gamehud) 
     { 
      if (gamehud.playerToGetStatisticsFrom.playerHP >= (gamehud.playerToGetStatisticsFrom.playerHPMaxValue * 0.8)) 
      { 
       gamehud.HP = 5; 
      } 
      if (gamehud.playerToGetStatisticsFrom.playerHP >= (gamehud.playerToGetStatisticsFrom.playerHPMaxValue * 0.6) & gamehud.playerToGetStatisticsFrom.playerHP < (gamehud.playerToGetStatisticsFrom.playerHPMaxValue * 0.8)) 
      { 
       gamehud.HP = 4; 
      } 
      if (gamehud.playerToGetStatisticsFrom.playerHP >= (gamehud.playerToGetStatisticsFrom.playerHPMaxValue * 0.4) & gamehud.playerToGetStatisticsFrom.playerHP < (gamehud.playerToGetStatisticsFrom.playerHPMaxValue * 0.6)) 
      { 
       gamehud.HP = 3; 
      } 
      if (gamehud.playerToGetStatisticsFrom.playerHP >= (gamehud.playerToGetStatisticsFrom.playerHPMaxValue * 0.2) & gamehud.playerToGetStatisticsFrom.playerHP < (gamehud.playerToGetStatisticsFrom.playerHPMaxValue * 0.4)) 
      { 
       gamehud.HP = 2; 
      } 
      if (gamehud.playerToGetStatisticsFrom.playerHP >= (gamehud.playerToGetStatisticsFrom.playerHPMaxValue * 0) & gamehud.playerToGetStatisticsFrom.playerHP < (gamehud.playerToGetStatisticsFrom.playerHPMaxValue * 0.2)) 
      { 
       gamehud.HP = 1; 
      } 
      if (gamehud.playerToGetStatisticsFrom.playerHP <= (gamehud.playerToGetStatisticsFrom.playerHPMaxValue * 0)) 
      { 
       gamehud.HP = 0; 
      } 
     } 

     public void RenderHUD() 
     { 
      Player guy = ThePlayer; 
      GameHUD ThisGamesHUD = initRenderHUD(guy, "PrimaryGameHUD"); 
      guy.playerHP = 100; 
      guy.playerHPMaxValue = 100; 
      guy.playerName = "The Hero!"; 
      string HPRepresenterHealthBoxIndex = @"C:\Users\Kent Brown\Documents\Visual Studio 2012\Projects\GameTesting\images\HUD\HealthRenderer\"; 
      PictureBox HPRepresenter = new PictureBox(); 
      HPRepresenter.AutoSize = true; 
      Debug.Print(from.Name); 
      from.Invoke(new Action(delegate() { from.Controls.Add(HPRepresenter); })); 
      string wait = "nil"; 
      Debug.Print("GAMESTATE HAS BEEN 2"); 
      while (GameState == 2) 
      { 
       // --- HP Renderer --- // 
       GetSettedHP(ThisGamesHUD); 
       wait = HPRepresenterHealthBoxIndex + Convert.ToString(ThisGamesHUD.HP) + ".png"; 
       from.Invoke(new Action(delegate() { HPRepresenter.Load(wait); })); 

       if (guy.playerHP <= (guy.playerHPMaxValue * 0)) 
       { 
        Label newLabel = new Label(); 
        newLabel.Text = "YOU HAVE DIED..."; 
        newLabel.Font = new Font(DeathFontFamily, 24, FontStyle.Regular); 
        from.Invoke(new Action(delegate() { from.Controls.Add(newLabel); })); 
       } 
      } 
     } 
    }; 

    public void StartHUDRenderThread(Form frm) 
    { 
     ThreadForRenderingHUD HUDRenderer = new ThreadForRenderingHUD(); 
     HUDRenderer.from = frm; 
     Thread RenderThread = new Thread(new ThreadStart(HUDRenderer.RenderHUD)); 
     RenderThread.Start(); 
     while (!RenderThread.IsAlive) ; 
    } 

    // ------------------------------- // 
    // -- Classes(Players/HUD, etc) -- // 
    // ------------------------------- // 

    public class GameHUD 
    { 
     public int HP; 
     public string Name; 
     public Player playerToGetStatisticsFrom; 
    } 

    public class Player 
    { 
     public int playerHP; 
     public int playerHPMaxValue; 
     public string playerName; 
    } 

    public static FontFamily DeathFontFamily = new FontFamily("Chiller"); 

    // ------------------------------- // 
    // ------ Stuff Declaration ------ // 
    // ------------------------------- // 

    public static Player ThePlayer = new Player(); 
    public static int GameState; 
    // GameState value references: 0) Initializing 1) Initialized 2) Playing //' 

当Visual Studio中抛出我的例外,它强调了这三条线中的Program.cs:

 Application.Run(new Form1()); 

它给出了这个异常,不应该Visual Studio挑选原因?如果任何人都能找到问题的根源,那么如果你能指出这一点会很好。谢谢!

+0

考虑到您遇到了特定错误,您是否也能够获取堆栈跟踪? –

+0

当你说,它是“给予例外”,它只是突出显示'Application.Run'行,并显示异常对话窗口标注的事情? –

+0

是的,这是唯一的事情。可悲的是,我没有办法从中得到任何错误日志。 – ApachePilotMPE

回答

1

小心static成员初始化。

相关问题