-1

基本的に 1 日中 Forms アプリケーションで作業した後、単純な UI といくつかのキーダウン イベントを作成しました。すごい!しかし今、それを実行しようとすると、突然 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 の次の 3 行が強調表示されます。

        Application.Run(new Form1());

例外が発生していますが、Visual Studio が原因を特定するべきではありませんか? 誰かが問題の根本を見つけることができる場合は、それを指摘していただければ幸いです。ありがとう!

4

1 に答える 1

1

staticメンバーの初期化に注意してください。

于 2013-06-04T03:48:36.590 に答える