错误消息:未找到可覆盖的合适方法

本文关键字:覆盖 方法 消息 错误 | 更新日期: 2023-09-27 18:19:19

嗨,我是一个新手,我一直在为我将要制作的游戏创建菜单导航,然后遇到了这个讨厌的错误信息,我知道这将是一个简单的问题,但对于我的生命,我看不到它,我会在下面发布代码

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);
        }
    }
}

错误消息:未找到可覆盖的合适方法

这个错误很容易解决,你有这个方法在你的Game_Screen类:

public virtual void UnloadContent() 
{
    content.Unload();
}

然后在Splash_ScreenTitle_Screen类中你有这个:

public override void UnLoadcontent()
{
    base.UnloadContent();
}

检查方法名!

您正在尝试用UnLoadcontent覆盖UnloadContent。这是行不通的,因为方法的名字是不同的,改变"Load"中的"L"的大小写,你就会没事了。

这是因为如果你重写了一些东西,那么方法的签名必须与父类上声明的完全相同,无论方法本身里面有什么。