【问题标题】:XNA 4.0. Null reference exceptionXNA 4.0。空引用异常
【发布时间】:2013-04-19 03:33:51
【问题描述】:
**public GraphicsDeviceManager graphics;
        public SpriteBatch spriteBatch;
        private int[] Heights;
        //public Texture2D Terrain;
        public Texture2D[] TerrainList;
        public Color[] Colors_TerrainTexture;
        public int [] Coordinates_X;
        Camera camera = new Camera();
        GraphicsDevice device;

        public MapFunctions()                
        {

        }   
 public void GenerateMap_Heights(int Map_Width,int Map_Height,float Relief,float Mountain_Peak_Height)
            {
                Heights = new int[Map_Width];
                Random randomizer = new Random();
                double rand1 = randomizer.NextDouble() + 1;
                double rand2 = randomizer.NextDouble() + 2;
                double rand3 = randomizer.NextDouble() + 3;

                float offset = Map_Height / 2;
                float peakheight = 324;
                float flatness = Mountain_Peak_Height;

                for (int x = 0; x < Map_Width; x++)
                {
                    double height = peakheight / rand1 * Math.Sin((float)x / flatness * rand1 + rand1);
                    height += peakheight / rand2 * Math.Sin((float)x / flatness * rand2 + rand2);
                    height += peakheight / rand3 * Math.Sin((float)x / flatness * rand3 + rand3);
                    height += offset;
                    Heights[x] = (int)height;
                }
            }

            public bool CanDraw = false;

            public void CreateTerrain(int Map_Width, int Map_Height,int TerrainTexture_Width,Color Color_Terrain,Color Color_Background)
            {            
                int TimesToLoop = (Map_Width / TerrainTexture_Width) + 1;
                int[] Coordinates_X = new int[TimesToLoop];
                device = graphics.GraphicsDevice;
                for (int b = 0; b <= TimesToLoop; b++)
                {
                    if (b == TimesToLoop)
                    {
                        Colors_TerrainTexture = new Color[Map_Width % TerrainTexture_Width * Map_Height];
                        TerrainTexture_Width = Map_Width % TerrainTexture_Width;
                    }
                    else 
                    {
                        Colors_TerrainTexture = new Color[TerrainTexture_Width * Map_Height];
                    }
                    Coordinates_X[b] = TerrainTexture_Width * b;
                    for (int x = 0; x < TerrainTexture_Width; x++)
                    {                    
                        for (int y = 0; y < Map_Height; y++)
                        {
                            if (y > Heights[x])
                                Colors_TerrainTexture[x + y * TerrainTexture_Width] = Color.Green;
                            else
                                Colors_TerrainTexture[x + y * TerrainTexture_Width] = Color.Transparent;
                        }
                    }                
                    Terrain = new Texture2D(device, TerrainTexture_Width, Map_Height, false, SurfaceFormat.Color);
                    Terrain.SetData(Colors_TerrainTexture);
                    TerrainList[b] = Terrain; //I get nullreference exception in this line
                }
                spriteBatch = new SpriteBatch(device);
                CanDraw = true;
            }**

TerrainList[b] = 地形; (这一行几乎在代码的末尾)我在这一行中得到空引用异常。我从 game1 LoadContent() 调用这两个函数(CreateTerrain 和 GenerateMap_Heights)。请帮忙,我在我的代码中找不到问题。

对不起,我的英语不好。

【问题讨论】:

    标签: xna


    【解决方案1】:

    你创建了你的地形列表,但从不初始化它......

    //Something like
    TerrainList = new Texture2D[];
    

    现在您尝试访问空集合的索引。

    【讨论】:

    • 哦。谢谢你。你是最好的。 :)
    【解决方案2】:

    您是否曾经创建过 TerrainList?我正在查看代码并没有看到它。

    Texture2D[] TerrainList = new Texture2D[10];
    

    现在您正尝试在数组中设置一个 Texture2D,但尚未创建。

    【讨论】:

      【解决方案3】:

      TerrainList 从未在提供的代码中初始化,因此当您使用 TerrainList[b] 对其进行索引时,它会返回 null。您需要创建Texture2D[] 的实例并将其分配给TerrainList。此外,您需要确保它至少具有 b 数量的元素,因为您使用 b 来索引数组。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-16
        • 2012-08-19
        • 2013-09-21
        • 2013-05-14
        • 1970-01-01
        相关资源
        最近更新 更多