我遇到protected SceneItem scene = null;
的问题,但我看不出原因,错误是:
可访问性不一致:字段类型'AsteroidsFinal.Helpers.SceneItem'比字段'AsteroidsFinal.Helpers.Screen.scene'`
更难访问
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace AsteroidsFinal.Helpers
{
abstract public class Screen
{
#region Variables
protected SceneItem scene = null;
protected Screen overlay;
protected SpriteBatch batch = null;
protected Game game = null;
#endregion
#region Properties
public Game GameInstance
{
get { return game; }
}
public SpriteBatch Sprites
{
get { return batch; }
}
#endregion
public Screen(AsteroidGame game)
{
this.game = game;
if (game != null)
{
IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)game.Services.GetService(typeof(IGraphicsDeviceService));
this.batch = new SpriteBatch(graphicsService.GraphicsDevice);
}
}
public virtual GameState Update(TimeSpan time, TimeSpan elapsedTime)
{
scene.Update(time, elapsedTime);
return (overlay == null) ? GameState.None : overlay.Update(time, elapsedTime);
}
public virtual void Render()
{
scene.Render();
if (overlay != null)
overlay.Render();
}
public virtual void Shutdown()
{
if (overlay != null)
overlay.Shutdown();
if (batch != null)
{
batch.Dispose();
batch = null;
}
}
public virtual void OnCreateDevice()
{
IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)game.Services.GetService(typeof(IGraphicsDeviceService));
batch = new SpriteBatch(graphicsService.GraphicsDevice);
}
}
}
答案 0 :(得分:6)
屏幕是一个公共类。因为它是一个公共类,所以您可以在内部创建派生类型,在程序集屏幕内部或外部,在程序集屏幕所在的内部创建派生类型。
“scene”受保护,这意味着,它可以从任何派生自它所在类的类中访问,在本例中是Screen,但是,你没有声明SceneItem,它是“场景“公开。如果开发人员从Screen派生,但是从程序集外部执行,那么他将无法访问SceneItem的类型,因为SceneItem很可能是内部的。
要解决此问题,您需要将屏幕上的辅助功能修改器限制为内部,或者您需要将SceneItem上的辅助功能修改器修改为公共。