C# - 由于其保护级别而无法访问
我不明白为什么会这样。我正在从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里的问题是
Game
内的所有成员都被标记为protected
或根本没有标记,默认为private
。这意味着只有Game
以及从它派生的任何类都可以访问这些protected
成员,而没有其他人可以访问private
。View
类型是完全独立的类型,因此无法访问任何protected
或private
的成员。要公开
private
字段,需要将它们标记为internal
或public
。这些成员中的大多数也被标记为
override
,因此您被锁定为protected
。但您可以使用另一个非受保护
成员来调用受保护
成员。从示例中尚不清楚您是否想要调用这些方法。The problem here is that all of the members inside of
Game
are marked asprotected
or not marked at all which defaults toprivate
. This means that onlyGame
and any classes which derive from it will have access to thoseprotected
members and no-one else toprivate
. The typeView
is a completely separate type and hence can't access any members that areprotected
orprivate
.To expose the
private
fields they need to be marked asinternal
orpublic
.Most of these members are also labeled as
override
hence you are locked intoprotected
. But you can use anothernon-protected
member to call into theprotected
. It's not clear from the sample if you want to invoke these methods or not.所有这些都隐式地是
private
:您需要将它们更改为
public
,这对于成员变量来说是一个坏主意。或者您可以创建属性来访问成员变量:All of this is implicitly
private
: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:默认情况下,在 C# 中,任何未明确定义可访问性的字段都被假定为私有字段。因此,您需要在
Game
类中执行以下操作:其次,值得注意的是,您的所有
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:Second, it's worth noting that all of your
Game
class's methods haveprotected
accessibility, which means that only classes that inherit fromGame
will be able to call those methods.