我有2个类引用主游戏功能中的列表。类Block和MovingPlatform都有一个包含所有对象的列表。
public List<Block> Blocks;
public List<MovingPlatform> Platforms;
我还有一个Collision Manager类,它使用2个列表来查看平台是否与块冲突,然后让平台朝另一个方向发展。
然后我将列表转移到类Collision Manager:
public class Collision_Manager
{
Game1 game1;
public void Initialize(Game1 game1)
{
this.game1 = new Game1();
}
public void Update(GameTime gameTime, Game1 game1)
{
this.game1 = game1;
for (int i = 0; i < game1.Blocks.Count; i++)
{
Rectangle BlockBounds = new Rectangle(
(int)game1.Blocks[i].Position.X,
(int)game1.Blocks[i].Position.Y,
game1.Blocks[i].Texture.Width,
game1.Blocks[i].Texture.Height);
Rectangle top = new Rectangle((int)BlockBounds.X + 5, (int)BlockBounds.Y - 10, BlockBounds.Width - 10, 10);
Rectangle bottom = new Rectangle((int)BlockBounds.X + 5, (int)BlockBounds.Y + BlockBounds.Height, BlockBounds.Width - 10, 10);
Rectangle left = new Rectangle((int)BlockBounds.X - 10, (int)BlockBounds.Y + 5, 10, BlockBounds.Height - 10);
Rectangle right = new Rectangle((int)BlockBounds.X + BlockBounds.Width, (int)BlockBounds.Y + 5, 10, BlockBounds.Height - 10);
for (int b = 0; b < game1.Platforms.Count; b++)
{
Rectangle PlatformBounds = new Rectangle(
(int)game1.Platforms[i].Position.X,
(int)game1.Platforms[i].Position.Y,
game1.Platforms[i].Texture.Width,
game1.Platforms[i].Texture.Height);
if (top.Intersects(new Rectangle(PlatformBounds.X, PlatformBounds.Y, PlatformBounds.Width, PlatformBounds.Height)) && game1.Blocks[i].BlockState == 3)
{
game1.Platforms[i].Thing = false;
}
if (bottom.Intersects(new Rectangle(PlatformBounds.X, PlatformBounds.Y, PlatformBounds.Width, PlatformBounds.Height)) && game1.Blocks[i].BlockState == 3)
{
game1.Platforms[i].Thing = true;
}
if (left.Intersects(new Rectangle(PlatformBounds.X, PlatformBounds.Y, PlatformBounds.Width, PlatformBounds.Height)) && game1.Blocks[i].BlockState == 3)
{
game1.Platforms[i].Thing = false;
}
if (right.Intersects(new Rectangle(PlatformBounds.X, PlatformBounds.Y, PlatformBounds.Width, PlatformBounds.Height)) && game1.Blocks[i].BlockState == 3)
{
game1.Platforms[i].Thing = true ;
}
}
}
}
}
出于某种原因,我收到错误:Inconsistent accessibility: field type 'System.Collections.Generic.List<Game.Block>' is less accessible than field 'Game.Game1.Blocks'
Inconsistent accessibility: field type 'System.Collections.Generic.List<Game.MovingPlatform>' is less accessible than field 'Game.Game1.Platforms'
如何解决问题?
答案 0 :(得分:2)
Block和Platform类必须是公共的,因为您在公共字段中使用它们。 或者,您可以使用与您在班级中使用的属性相同或更低的属性访问者。 例如,如果您的类被声明为内部使用,也是您的字段内部使用。
答案 1 :(得分:0)
您的课程Block
和MovingPlatform
的访问权限低于您的public
字段List<Block> Blocks
和List<MovingPlatform> Platforms
。
您可能还没有为您的班级Block
和MovingPlatform
定义任何访问说明符,因此默认情况下它们被视为internal
,这比公开版更容易访问。
您可以将这些课程设为public
来解决问题,也可以将字段标记为internal
。您应该根据您的要求决定访问属性/类。您可能会看到Access Modifiers (C# Reference)