突然发生System.TypeInitializationException,Visual Studio未指出任何错误

时间:2013-06-04 03:09:00

标签: c# exception

在使用Forms Application基本上整天之后,我创建了一个简单的UI和一些keydown事件。大!但现在,我去运行它,它突然开始抛出System.TypeInitializationException。通常情况下,我可能会自己找出这样的问题(使用Visual Studio的帮助),但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是否应该选择原因?如果有人能找到问题的根源,那么如果你能指出它就会很棒。谢谢!

1 个答案:

答案 0 :(得分:1)

注意static成员初始化。