【问题标题】:Inconsistent accessibility: field type is less accessible可访问性不一致:字段类型难以访问
【发布时间】:2014-04-22 14:35:43
【问题描述】:

我有 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&lt;Game.Block&gt;' is less accessible than field 'Game.Game1.Blocks'

Inconsistent accessibility: field type 'System.Collections.Generic.List&lt;Game.MovingPlatform&gt;' is less accessible than field 'Game.Game1.Platforms'

如何解决我的问题?

【问题讨论】:

  • 你在哪一行得到错误?
  • 它不允许我调试游戏,它会说你想使用最后一个版本。
  • 但它仍然应该告诉您错误发生在代码的哪一行...
  • 公开所有内容,然后将它们恢复为以前的可访问性修饰符,直到错误再次弹出
  • 错误是不言自明的。也许您的班级 Game.Block 声明为内部。

标签: c# list xna collision-detection


【解决方案1】:

Block 和 Platform 类必须是公共的,因为您在公共字段中使用它们。 或者,您可以为该属性使用与您在课堂上使用的相同或更低的访问器。 例如,如果您的类被声明为内部使用,那么您的字段也是内部使用。

【讨论】:

    【解决方案2】:

    您的课程BlockMovingPlatform 的访问权限低于public 字段List&lt;Block&gt; BlocksList&lt;MovingPlatform&gt; Platforms

    您可能还没有为您的类BlockMovingPlatform 定义任何访问说明符,因此默认情况下它们被视为internal,它不像公共那样可访问。

    您可以通过将这些类设为public 来解决此问题,或者您可以将您的字段标记为internal。您应该根据自己的要求决定对属性/类的访问。你可能会看到Access Modifiers (C# Reference)

    【讨论】:

      猜你喜欢
      • 2012-11-19
      • 2023-03-24
      • 1970-01-01
      • 2012-10-11
      • 2014-06-25
      • 1970-01-01
      • 2018-03-07
      • 2012-10-27
      相关资源
      最近更新 更多