当我将Texture2D作为参数传递给类时,此纹理为空

时间:2015-06-02 13:50:40

标签: c# menu xna monogame

这是我遇到问题的代码:

namespace Menu_test
{
    /// <summary>
    /// The main menu screen is the first thing displayed when the game starts up.
    /// </summary>
    class MainMenuScreen : MenuScreen
    {
        #region Initialization
        ContentManager content;
        Texture2D playgame;
        Texture2D exit;

        /// <summary>
        /// Constructor fills in the menu contents.
        /// </summary>
        public MainMenuScreen()
            : base()
        {
            // Create our menu entries.
            MenuEntry playGameMenuEntry = new MenuEntry(playgame);
            MenuEntry exitMenuEntry = new MenuEntry(exit);

            // Hook up menu event handlers.
            playGameMenuEntry.Selected += PlayGameMenuEntrySelected;
            exitMenuEntry.Selected += OnCancel;

            // Add entries to the menu.
            MenuEntries.Add(playGameMenuEntry);
            MenuEntries.Add(exitMenuEntry);
        }

        public override void LoadContent()
        {
            if (content == null)
                content = new ContentManager(ScreenManager.Game.Services, "Content");
            Art.Load(content);

            playgame = Art.PlayGame;
            exit = Art.Exit;
            if (playgame==null)
                throw new ArgumentNullException();
        }


        /// <summary>
        /// Unloads graphics content for this screen.
        /// </summary>
        public override void UnloadContent()
        {
            content.Unload();
        }

        #endregion

        #region Handle Input


        /// <summary>
        /// Event handler for when the Play Game menu entry is selected.
        /// </summary>
        void PlayGameMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            LoadingScreen.Load(ScreenManager, true, e.PlayerIndex,
                               new GameplayScreen());
        }


        /// <summary>
        /// Event handler for when the Options menu entry is selected.
        /// </summary>


        /// <summary>
        /// When the user cancels the main menu, ask if they want to exit the sample.
        /// </summary>
        protected override void OnCancel(PlayerIndex playerIndex)
        {
            ScreenManager.Game.Exit();
        }




        #endregion
    }
}

因此MainMenuScreen() playgame nullLoadContent()null Texture2D不是MenuEntry。基本上我想要做的是将.forum-table tr:nth-of-type(1) .forum-number-topics作为参数传递给<table class="forum-table forum-table-forums"> <thead class="forum-header"> <tr> <th class="forum-icon">&nbsp;</th> <th class="forum-name">Forum</th> <th class="forum-topics">Topics</th> <th class="forum-posts">Posts</th> <th class="forum-last-post">Last post</th> </tr> </thead> <tbody id="forum-table-12892-content"> <tr class="forum-row even container-12897-child" id="forum-12897"> <td class="forum-list-icon forum-list-icon-default"> <span class="forum-list-icon-wrapper"><span>No new</span></span> </td> <td colspan="1" class="forum-details"> <div class="forum-name"> <a href="/forums/settimana-1-1">Settimana 1</a> </div> </td> <td class="forum-number-topics"> <div class="forum-number-topics">20</div> </td> <td class="forum-number-posts">171</td> <td class="forum-last-reply">n/a</td> </tr> <tr class="forum-row odd container-13043-child" id="forum-13043"> <td class="forum-list-icon forum-list-icon-default"> <span class="forum-list-icon-wrapper"><span>No new</span></span> </td> <td colspan="1" class="forum-details"> <div class="forum-name"> <a href="/forums/settimana-2-0">Settimana 2</a> </div> </td> <td class="forum-number-topics"> <div class="forum-number-topics">21</div> </td> <td class="forum-number-posts">143</td> <td class="forum-last-reply">n/a</td> </tr> <tr class="forum-row even container-13107-child" id="forum-13107"> <td class="forum-list-icon forum-list-icon-default"> <span class="forum-list-icon-wrapper"><span>No new</span></span> </td> <td colspan="1" class="forum-details"> <div class="forum-name"> <a href="/forums/settimana-3-0">Settimana 3</a> </div> </td> <td class="forum-number-topics"> <div class="forum-number-topics">20</div> </td> <td class="forum-number-posts">91</td> <td class="forum-last-reply">n/a</td> </tr> </tbody> </table> 类,但是在传递给类之前纹理为null。 如果您想尝试运行它,可以下载完整的项目here

感谢您的阅读。

1 个答案:

答案 0 :(得分:0)

初始化MainMenuScreen后,XNA会调用LoadContent。您可能必须将playGame对象的使用推迟到加载纹理的时刻或初始化LoadContent中的MenuEntries。