C# 当我将Texture2D作为参数传递给类时,该纹理为null
下面是我遇到问题的代码:C# 当我将Texture2D作为参数传递给类时,该纹理为null,c#,menu,xna,monogame,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
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
}
}
名称空间菜单\u测试
{
///
///主菜单屏幕是游戏启动时显示的第一个内容。
///
类MainMenuScreen:MenuScreen
{
#区域初始化
ContentManager内容;
纹理2D游戏;
纹理2d出口;
///
///构造函数填充菜单内容。
///
公共主菜单屏幕()
:base()
{
//创建菜单项。
MenuEntry PLAYGAMENUENTRY=新MenuEntry(游戏机);
MenuEntry exitMenuEntry=新MenuEntry(退出);
//连接菜单事件处理程序。
PlayGameMenutery.Selected+=PlayGameMenutery Selected;
exitMenuEntry.Selected+=OnCancel;
//在菜单中添加条目。
添加(PlayGameMenutery);
菜单项。添加(exitMenuEntry);
}
公共覆盖无效加载内容()
{
if(content==null)
内容=新的ContentManager(ScreenManager.Game.Services,“内容”);
艺术负荷(内容);
游戏=艺术。游戏;
退出=艺术退出;
如果(playgame==null)
抛出新ArgumentNullException();
}
///
///卸载此屏幕的图形内容。
///
公共覆盖无效内容()
{
content.Unload();
}
#端区
#区域句柄输入
///
///选择“玩游戏”菜单项时的事件处理程序。
///
void PlayGameMenuterySelected(对象发送方,PlayerIndexEventArgs e)
{
LoadingScreen.Load(ScreenManager,true,e.PlayerIndex,
新游戏屏幕());
}
///
///选择选项菜单项时的事件处理程序。
///
///
///当用户取消主菜单时,询问他们是否要退出示例。
///
受保护的覆盖无效OnCancel(PlayerIndex PlayerIndex)
{
ScreenManager.Game.Exit();
}
#端区
}
}
因此,在MainMenuScreen()。基本上,我想做的是将Texture2D
作为参数传递给MenuEntry
类,但即使在传递给类之前,纹理也是空的。
如果您想尝试运行整个项目,可以下载它
感谢阅读。在初始化MainMenuScreen
后,XNA将调用LoadContent。您可能必须将playGame
对象的使用推迟到纹理加载或在LoadContent
中初始化菜单项的那一刻。好吧,我必须非常感谢您,因为我已经讨论这个问题三天了。我不知道在LoadContent
中初始化您的MenuEntries
是否是正确的方法,但至少它是有效的!再次感谢你,伙计@这实际上是鸡或蛋的问题。菜单项需要来自构造函数后调用的方法的内容。您可以在没有纹理的情况下初始化它们,然后在LoadContent中为它们指定一个纹理,您只需在LoadContent中使用刚刚加载的纹理实例化它们即可。