嗨我有一个新手我一直在创建一个游戏的菜单导航即将制作并遇到这个讨厌的错误信息我知道这将是一个简单的问题,但对于我的生活我不能看到它生病发布以下代码
namespace Platformer
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
Screen_Manager.Instance.Initiaize();
Screen_Manager.Instance.Dimensions = new Vector2(800, 600);
graphics.PreferredBackBufferWidth = (int)Screen_Manager.Instance.Dimensions.X;
graphics.PreferredBackBufferHeight = (int)Screen_Manager.Instance.Dimensions.Y;
graphics.ApplyChanges();
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
Screen_Manager.Instance.LoadContent(Content);
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
Screen_Manager.Instance.Update(gameTime);
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
Screen_Manager.Instance.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
namespace Platformer
{
public class Screen_Manager
{
#region variables
ContentManager content; // creating custom ContentManger
Game_Screen currentScreen; // screen being currently displayed
Game_Screen newScreen; // new screen taking effect
private static Screen_Manager instance; // Screen Manger instance
// Dictionary<string, Game_Screen> screens = new Dictionary<string, Game_Screen>(); // Game Screen storage
Stack<Game_Screen> screenStack = new Stack<Game_Screen>(); // lets me know order of screens Stack
Vector2 dimensions; // width&Height of Screens
#endregion
#region Properties
public static Screen_Manager Instance
{
get
{
if (instance == null)
instance = new Screen_Manager();
return instance;
}
}
public Vector2 Dimensions
{
get { return dimensions; }
set { dimensions = value; }
}
#endregion
#region Main Methods
public void AddScreen(Game_Screen screen)
{
newScreen = screen;
screenStack.Push(screen);
currentScreen.UnloadContent();
currentScreen = newScreen;
currentScreen.LoadContent(content);
}
public void Initiaize()
{
currentScreen = new Splash_Screen();
}
public void LoadContent(ContentManager Content)
{
content = new ContentManager(Content.ServiceProvider, "Content");
currentScreen.LoadContent(Content);
}
public virtual void Update(GameTime gameTime)
{
currentScreen.Update(gameTime);
}
public virtual void Draw(SpriteBatch spriteBatch)
{
currentScreen.Draw(spriteBatch);
}
#endregion
}
}
namespace Platformer
{
public class Game_Screen
{
protected ContentManager content;
public virtual void LoadContent(ContentManager Content)
{
content = new ContentManager(Content.ServiceProvider, "content");
}
public virtual void UnloadContent()
{
content.Unload();
}
public virtual void Update(GameTime gameTime)
{
}
public virtual void Draw(SpriteBatch spriteBatch)
{
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace Platformer
{
public class Splash_Screen : Game_Screen
{
KeyboardState keystate;
SpriteFont font;
public override void LoadContent(ContentManager Content)
{
base.LoadContent(Content);
if (font == null)
font = content.Load<SpriteFont>("Font1");
}
public override void UnLoadContent()
{
base.UnloadContent();
}
public override void Update(GameTime gameTime)
{
keystate = Keyboard.GetState();
if (keystate.IsKeyDown(Keys.Z))
Screen_Manager.Instance.AddScreen(new Title_Screen());
}
public override void Draw(SpriteBatch spriteBatch)
{
spriteBatch.DrawString(font, "SplashScreen",
new Vector2(100, 100), Color.Black);
}
}
}
namespace Platformer
{
public class Title_Screen : Game_Screen
{
KeyboardState keystate;
SpriteFont font;
public override void LoadContent(ContentManager Content)
{
base.LoadContent(Content);
if (font == null)
font = content.Load<SpriteFont>("Font1");
}
public override void UnLoadcontent()
{
base.UnloadContent();
}
public override void Update(GameTime gameTime)
{
keystate = Keyboard.GetState();
if (keystate.IsKeyDown(Keys.Enter))
Screen_Manager.Instance.AddScreen(new Splash_Screen());
}
public override void Draw(SpriteBatch spriteBatch)
{
spriteBatch.DrawString(font, "Title Screen",
new Vector2(100, 100), Color.Black);
}
}
}
答案 0 :(得分:2)
错误非常简单,您可以在Game_Screen
类中使用此方法:
public virtual void UnloadContent()
{
content.Unload();
}
然后在Splash_Screen
和Title_Screen
课程中你都有这个:
public override void UnLoadcontent()
{
base.UnloadContent();
}
检查方法的名称!
您正尝试使用UnloadContent
覆盖UnLoadcontent
。这将永远不会起作用,因为方法的名称不同,在“加载”中更改“L”的大小写,你会没事的。
这是因为如果你覆盖某些东西,那么方法的签名必须与父类中声明的完全相同,无论方法本身内部是什么。