平铺世界屏幕系统的问题?

时间:2013-12-14 11:24:31

标签: c# xna-4.0 tile

我试图为我的游戏制作一个基于图块的世界屏幕,这里有三个类我

namespace WindowsGame1
{
 public enum Tiletype
{Grass,
    Water,
    Foothill,
    Mountain

}
public struct Tile
{
    public  Tiletype TerrainType { get; set; }
    public Texture2D TileGFX { get; set; }
    public Rectangle SrcRect { get; set; }
    public Vector2 Location { get; set; }
    public int AniFrame { get; set; }

    // TILE ACTIONS
    public bool IsActivated { get; set; }
    public bool IsBlocked { get; set; }
    public bool IsTouchTrigger { get; set; }
    public bool IsStepTrigger { get; set; }
}
}

MapBase类

    public class MapBase
{

    public Tile[,] TileList = new Tile[1, 1];
    public MapBase(int width, int height, Vector2 start)
    {
        TileList = new Tile[width + 1, height + 1];

        // TEMPORARY MAP
        for (int X = 0; X <= width; X++)
        {
            for (int Y = 0; Y <= height; Y++)
            {
                TileList[X, Y] = new Tile();
                var _with1 = TileList[X, Y];
                _with1.TerrainType = Tiletype.Water;
                _with1.TileGFX = Textures.World;
                _with1.AniFrame = 0;
                _with1.IsBlocked = true;
            }
        }

        // SIMPLE ISLAND
        for (int z = 22; z <= 33; z++)
        {
            for (int c = 22; c <= 31; c++)
            {
                TileList[z, c].TerrainType = Tiletype.Grass;
            }
        }

        TileList[27, 25].TerrainType = Tiletype.Foothill;
        TileList[28, 25].TerrainType = Tiletype.Foothill;
        TileList[27, 24].TerrainType = Tiletype.Foothill;
        TileList[28, 26].TerrainType = Tiletype.Mountain;


        for (int x = 0; x <= width; x++)
        {
            for (int y = 0; y <= height; y++)
            {
                TileList[x, y].SrcRect = GetTileSource(TileList[x, y].TerrainType);
            }
        }

    }

GetTitleSource函数

    private Rectangle GetTileSource(Tiletype TType)
    {
        Rectangle sRect = default(Rectangle);
        switch (TType)
        {
            case Tiletype.Grass:
                sRect = new Rectangle(0, 0, 16, 16);
                break;
            case Tiletype.Water:
                sRect = new Rectangle(0, 80, 16, 16);
                break;
            case Tiletype.Foothill:
                sRect = new Rectangle(48, 0, 16, 16);
                break;
            case Tiletype.Mountain:
                sRect = new Rectangle(32, 0, 16, 16);
                break;
        }
        return sRect;
    }
}

Class WorldScreen

 class Worldscreen : BaseScreen
{
    public int MapWidth = 100;
    public int MapHeight = 100;
    public int TileSize = 32;
    public int MapX = 20;
    public int MapY = 19;
    public MapBase Map = new MapBase(100, 100, new Vector2(0, 0));

    public Worldscreen(GraphicsDevice device)
        : base(device, "WorldScreen")
    {
        Map = new MapBase(MapWidth, MapHeight, new Vector2(0, 0));
    }

绘制功能

    public override void Draw(GameTime gameTime)
    {
        base.Draw(gameTime);
        Globals.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullNone);

        //DRAW TILE LAYER
        for (int DrawX = -1; DrawX <= 16; DrawX++)
        {
            for (int DrawY = -1; DrawY <= 15; DrawY++)
            {
                int x = DrawX + MapX;
                int y = DrawY + MapY;

                if (x > 0 & x <= MapWidth & y >= 0 & y <= MapHeight)
                {
                    Globals.spriteBatch.Draw(Map.TileList[x,y].TileGFX, new Rectangle(DrawX * TileSize, DrawY * TileSize, TileSize, TileSize), Map.TileList[x, y].SrcRect, Color.White);
                    // VIEW COORDINATES ON TILE
                    //Globals.SpriteBatch.DrawString(Fonts.Arial_8, "X:" & x & vbCrLf & "Y:" & y, New Vector2(DrawX * TileSize, DrawY * TileSize), Color.Black)
                }
            }
        }
        Globals.spriteBatch.End();
    }
}

当我尝试运行draw void时,它会在世界屏幕类上出现此错误 此方法不接受此参数的null。 参数名称:texture

有什么方法可以解决这个问题,或者我必须重做这些课程。

1 个答案:

答案 0 :(得分:0)

您获得了自己调试所需的所有信息:

在世界屏幕类上有一个纹理= null的方法。

逐步调试并找出哪一个,除非它是一个简单的错误我们可以找到你的代码,但它往往不那么容易。 (例如:你有一个不是实例的名单吗?)

但由于它的方法是参数化的...接受纹理作为参数的唯一方法是GetTileSource。

如此迅速可以解释它......我会检查两件事:

A-你确定你的循环有正确的界限吗?你可能只是要求该数组中的一个tile,因为它超出了界限。 (数组是0 base,你循环到&lt; = height而不是&lt; height)

B-我要检查的第二件事(因为这是一个纹理),你没有加载你使用的那些纹理...