C# - 由于其保护级别而无法访问

发布于 2024-12-11 21:48:57 字数 3754 浏览 0 评论 0原文

我不明白为什么会这样。我正在从 Game 类创建一个 View 类,然后尝试从 Game 中的 View 调用一个方法并将整个游戏对象作为参数发送给它。如果我发送单个变量作为参数,那么它工作没有问题,但我想发送整个游戏对象,以便只传递一件事。

游戏类

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Airfield
{
    public class Game : Microsoft.Xna.Framework.Game
    {
        // Device Objects
        GraphicsDevice device = null;
        GraphicsDeviceManager graphics = null;
        MouseState mouse;

        // Game Objects
        SpriteBatch spriteBatch;
        Texture2D b = null;
        View view = null;

        // Arrays
        Buoy [] buoy = new Buoy[3];
        Plane [] plane = new Plane[3];

        // Variables
        bool selected = false;
        int index = 0;

        public Game()
        {
            graphics = new GraphicsDeviceManager(this);
        }

        protected override void Initialize()
        {
            Content.RootDirectory = "Content"; 
            this.IsMouseVisible = true;
            graphics.PreferredBackBufferWidth = 800;
            graphics.PreferredBackBufferHeight = 600;
            graphics.ApplyChanges();
            base.Initialize();

            view = new View();

            for (index = 0; index < buoy.Length; index++)
            {
                buoy[index] = new Buoy();
                plane[index] = buoy[index].CreatePlane();
            }
        }

        protected override void LoadContent()
        {
            device = graphics.GraphicsDevice;
            spriteBatch = new SpriteBatch(device);
            b = Content.Load<Texture2D>("buoy");
        }

        protected override void UnloadContent()
        {

        }

        protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            mouse = Mouse.GetState();

            if (selected == true || mouse.X >= buoy[0].position.X - 3 && mouse.X <= buoy[0].position.X + 19 && mouse.Y >= buoy[0].position.Y - 3 && mouse.Y <= buoy[0].position.Y + 19)
            {
                if (mouse.LeftButton == ButtonState.Pressed)
                {
                    selected = true;
                    buoy[0].position= new Vector2(mouse.X - 8, mouse.Y - 8);
                }

                else
                {
                    selected = false;
                }
            }
        }

        protected override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            view.DrawScreen(this);
        }
    }
}

视图类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Airfield
{
    class View
    {
        public View()
        {

        }

        public void DrawScreen(Game game)
        {
            game.device.Clear(Color.CornflowerBlue);

            game.spriteBatch.Begin();
            DrawBuoys(game);
            game.spriteBatch.End();
        }

        public void DrawBuoys(Game game)
        {
            for (int x = 0; x < game.buoy.Length; x++)
            {
                game.buoy[x].DrawBuoy(game.spriteBatch, game.b);
            }
        }
    }
}

基本上,每次我尝试使用来自游戏的东西时,我都会收到错误。

I can't figure out why this is the case. I'm creating a View class from Game class and then I'm trying to call a method from View in Game and send it the entire game object as a parameter. If I send single variables as a parameter then it work's no problem but I wanted to send the whole Game object so that only one thing would be passed.

Game class

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Airfield
{
    public class Game : Microsoft.Xna.Framework.Game
    {
        // Device Objects
        GraphicsDevice device = null;
        GraphicsDeviceManager graphics = null;
        MouseState mouse;

        // Game Objects
        SpriteBatch spriteBatch;
        Texture2D b = null;
        View view = null;

        // Arrays
        Buoy [] buoy = new Buoy[3];
        Plane [] plane = new Plane[3];

        // Variables
        bool selected = false;
        int index = 0;

        public Game()
        {
            graphics = new GraphicsDeviceManager(this);
        }

        protected override void Initialize()
        {
            Content.RootDirectory = "Content"; 
            this.IsMouseVisible = true;
            graphics.PreferredBackBufferWidth = 800;
            graphics.PreferredBackBufferHeight = 600;
            graphics.ApplyChanges();
            base.Initialize();

            view = new View();

            for (index = 0; index < buoy.Length; index++)
            {
                buoy[index] = new Buoy();
                plane[index] = buoy[index].CreatePlane();
            }
        }

        protected override void LoadContent()
        {
            device = graphics.GraphicsDevice;
            spriteBatch = new SpriteBatch(device);
            b = Content.Load<Texture2D>("buoy");
        }

        protected override void UnloadContent()
        {

        }

        protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            mouse = Mouse.GetState();

            if (selected == true || mouse.X >= buoy[0].position.X - 3 && mouse.X <= buoy[0].position.X + 19 && mouse.Y >= buoy[0].position.Y - 3 && mouse.Y <= buoy[0].position.Y + 19)
            {
                if (mouse.LeftButton == ButtonState.Pressed)
                {
                    selected = true;
                    buoy[0].position= new Vector2(mouse.X - 8, mouse.Y - 8);
                }

                else
                {
                    selected = false;
                }
            }
        }

        protected override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            view.DrawScreen(this);
        }
    }
}

View Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Airfield
{
    class View
    {
        public View()
        {

        }

        public void DrawScreen(Game game)
        {
            game.device.Clear(Color.CornflowerBlue);

            game.spriteBatch.Begin();
            DrawBuoys(game);
            game.spriteBatch.End();
        }

        public void DrawBuoys(Game game)
        {
            for (int x = 0; x < game.buoy.Length; x++)
            {
                game.buoy[x].DrawBuoy(game.spriteBatch, game.b);
            }
        }
    }
}

Basically I get the error for every time that I try and use something that has come from Game.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青丝拂面 2024-12-18 21:48:57

这里的问题是 Game 内的所有成员都被标记为 protected 或根本没有标记,默认为 private。这意味着只有 Game 以及从它派生的任何类都可以访问这些 protected 成员,而没有其他人可以访问 privateView 类型是完全独立的类型,因此无法访问任何protectedprivate 的成员。

要公开 private 字段,需要将它们标记为 internalpublic

public class Game : Microsoft.Xna.Framework.Game
{
  ...
  public SpriteBatch spriteBatch;
}

这些成员中的大多数也被标记为override,因此您被锁定为protected。但您可以使用另一个非受保护成员来调用受保护成员。从示例中尚不清楚您是否想要调用这些方法。

The problem here is that all of the members inside of Game are marked as protected or not marked at all which defaults to private. This means that only Game and any classes which derive from it will have access to those protected members and no-one else to private. The type View is a completely separate type and hence can't access any members that are protected or private.

To expose the private fields they need to be marked as internal or public.

public class Game : Microsoft.Xna.Framework.Game
{
  ...
  public SpriteBatch spriteBatch;
}

Most of these members are also labeled as override hence you are locked into protected. But you can use another non-protected member to call into the protected. It's not clear from the sample if you want to invoke these methods or not.

背叛残局 2024-12-18 21:48:57

所有这些都隐式地是private

// Device Objects
GraphicsDevice device = null;
GraphicsDeviceManager graphics = null;
MouseState mouse;

// Game Objects
SpriteBatch spriteBatch;
Texture2D b = null;
View view = null;

// Arrays
Buoy [] buoy = new Buoy[3];
Plane [] plane = new Plane[3];

// Variables
bool selected = false;
int index = 0;

您需要将它们更改为public,这对于成员变量来说是一个坏主意。或者您可以创建属性来访问成员变量:

public SpriteBatch SpriteBatch {
    get { return this.spriteBatch; }
    protected set { this.spriteBatch = value; }
}

All of this is implicitly private:

// Device Objects
GraphicsDevice device = null;
GraphicsDeviceManager graphics = null;
MouseState mouse;

// Game Objects
SpriteBatch spriteBatch;
Texture2D b = null;
View view = null;

// Arrays
Buoy [] buoy = new Buoy[3];
Plane [] plane = new Plane[3];

// Variables
bool selected = false;
int index = 0;

You'd need to change them to public, which is a bad idea for member variables. Or you can create properties to access the member variables:

public SpriteBatch SpriteBatch {
    get { return this.spriteBatch; }
    protected set { this.spriteBatch = value; }
}
倒数 2024-12-18 21:48:57

默认情况下,在 C# 中,任何未明确定义可访问性的字段都被假定为私有字段。因此,您需要在 Game 类中执行以下操作:

    // Device Objects
    public GraphicsDevice device = null;
    public GraphicsDeviceManager graphics = null;
    public MouseState mouse;

    // etc... for your other fields

其次,值得注意的是,您的所有 Game 类的方法都具有 protected 可访问性,这意味着只有继承自 Game 的类才能调用这些方法。

By default in C# any fields that do not have accessibility explicitly defined are assumed to be private. So, you will need to do the following in your Game class:

    // Device Objects
    public GraphicsDevice device = null;
    public GraphicsDeviceManager graphics = null;
    public MouseState mouse;

    // etc... for your other fields

Second, it's worth noting that all of your Game class's methods have protected accessibility, which means that only classes that inherit from Game will be able to call those methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文