0

I've started a new XNA project and am having some issues communicating between classes. Essentially, I'm laying down framework for a tile-based platformer and have two very simple classes at the moment.

One class, Tile(Tile.cs) contains and enum, named TileCollision and a struct named Tile.

The other, Level(Level.cs). Any time I try to reference TileCollision or try to create a Tile, it says it doesn't exist in the current context.

Is there anything else I need to do to get these two classes to talk? They're in the same namespace and don't need references added since they're not compiled DLL's or anything. Not sure what I've missed.

Here's the code for Tile.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace PoriPlatformer
{
class Tile
{
    // Controls the collision detection and response behavior of a tile.
    enum TileCollision
    {
        // A passable tile is one which does not hinder player motion at all, Example: Air
        Passable = 0,

        // An impassible tile is one which does not allow the player to move through it at all
        // It is completely solid.
        Impassable = 1,

        // A platform tile is one which behaves like a passable tile except when the player
        // is above it. A player can jump up through the platform as well as move past it
        // to the left and right, but can not fall through the top of it. 
        Platform = 2,
    }

    struct Tile
    {
        public Texture2D Texture;
        public TileCollision Collision;

        public const int Width = 40;
        public const int Height = 32;

        public static readonly Vector2 Size = new Vector2(Width, Height);


        // Constructs a new tile
        public Tile(Texture2D texture, TileCollision collision)
        {
            Texture = texture;
            Collision = collision;
        }


    }

}
 }

Here's the offending code in Level.cs:

// Loads an individual tile's appearance and behavior.
    private Tile LoadTile(char tileType, int x, int y)
    {
        switch (tileType)
        {
            // Blank space
            case '.':
                return new Tile(null, TileCollision.Passable);

            // Passable platform
            case '~':
                return LoadTile("platform", TileCollision.Platform);

            // Impassable block
            case '#':
                return LoadTile("block", TileCollision.Impassable);

            case '_':
                return LoadTile("ground", TileCollision.Impassable);


            default:
                throw new NotSupportedException(String.Format("Unsupported tile type character '{0}' at position {1}, {2}.", tileType, x, y));
        }
    }

underlined portions in Level.cs would be TileCollision

4

3 回答 3

2

请看这篇文章

在 Tile 类中定义的枚举和结构的默认范围是私有的,因此它们只能从 Tile 类中访问。您需要将它们更改为 internal 或 public 以便它们在同一命名空间中的另一个类中可见。

于 2013-04-30T20:02:21.347 回答
2

正如我在评论中所说,TileCollision是 class 的成员Tile。要访问它,您必须有一个Tile.

更好的方法是将枚举声明移到TileCollision类之外,如下所示:

public enum TileCollision
{
       Passable = 0,
       Impassable = 1,
       Platform = 2,
}

class Tile { ... }

然后,假设 Level.cs 在同一个命名空间中,语句如下:

return new Tile(null, TileCollision.Passable);

应该管用。

于 2013-04-30T20:02:33.037 回答
1

TileCollision 和 Tile(嵌套结构)需要公开才能在类之外看到。此外,您需要先使用外部类来引用它们:

...
return LoadTile("block", Tile.TileCollision.Impassable);
...

...
new Tile.Tile();
...
于 2013-04-30T20:06:18.537 回答