加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 01:48:28 所属栏目:百科 来源:网络整理
导读:我无法弄清楚为什么会这样.我正在从Game类创建一个View类,然后我尝试从View in Game中调用一个方法,并将整个游戏对象作为参数发送给它.如果我发送单个变量作为参数然后它没有问题,但我想发送整个Game对象,以便只传递一件事. 游戏课 using System;using Syste
我无法弄清楚为什么会这样.我正在从Game类创建一个View类,然后我尝试从View in Game中调用一个方法,并将整个游戏对象作为参数发送给它.如果我发送单个变量作为参数然后它没有问题,但我想发送整个Game对象,以便只传递一件事.

游戏课

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

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

解决方法

这里的问题是游戏中的所有成员都被标记为受保护或根本没有标记为默认为私有.这意味着只有Game和从中派生的任何类都可以访问这些受保护的成员而没有其他人可以访问私有.类型View是完全独立的类型,因此无法访问受保护或私有的任何成员.

要公开私有字段,需要将它们标记为内部或公共字段.

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

这些成员中的大多数也被标记为覆盖,因此您将被锁定为受保护.但是您可以使用另一个不受保护的成员来调用受保护的成员.从样本中不清楚是否要调用这些方法.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读