为什么指数超出范围?

时间:2013-07-24 23:00:19

标签: c# xna

我最近决定尝试做一个基于平铺的“游戏”。 我跟着来自YouTube的Nick Gravelyn和​​ouyyu91的教程,无论我做什么,它 一直告诉我Map.Coords的索引超出范围。 (我希望你能从这段代码中读到一些内容,我并不太关心架构)

游戏类:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Platformer
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class MainGame : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;



        TileMap Map = new TileMap("Daniel", "Beastiality 1", "1/EASY");
        int TileWidth, TileHeight;

        public MainGame()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);


            graphics.PreferredBackBufferWidth = 800;
            graphics.PreferredBackBufferHeight = 600;

            Texture2D tex_Air = Content.Load<Texture2D>("Tiles/tex_air");
            Texture2D tex_Ground = Content.Load<Texture2D>("Tiles/tex_ground");
            Texture2D tex_Wall = Content.Load<Texture2D>("Tiles/tex_wall");
            Texture2D tex_Celldoor = Content.Load<Texture2D>("Tiles/tex_celldoor");
            Texture2D tex_Woodenchest = Content.Load<Texture2D>("Tiles/tex_woodenchest");

            Program.Tiles.Add(tex_Air);
            Program.Tiles.Add(tex_Ground);
            Program.Tiles.Add(tex_Wall);

            TileWidth = graphics.PreferredBackBufferWidth / 50;
            TileHeight = TileWidth;

            Map.Coords = new int[,]
            {

                {2,2,2,2,2,2,2,0,0,0},
                {2,0,0,4,0,0,2,0,0,0},
                {2,0,0,0,0,0,2,0,0,0},
                {2,2,2,3,2,2,2,0,0,0},
                {2,0,0,0,0,0,2,0,0,0},
                {2,0,0,0,0,0,2,0,0,0},
                {0,0,0,0,0,0,0,0,0,0},
                {0,0,0,0,0,0,0,0,0,0},
                {0,0,0,0,0,0,0,0,0,0},
                {0,0,0,0,0,0,0,0,0,0},

            };
            Map.Width = Map.Coords.GetLength(1);
            Map.Height = Map.Coords.GetLength(0);







            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here


            spriteBatch.Begin();

            for (int x = 0; x < Map.Coords.GetLength(1); x++)
            {

                for (int y = 0; y < Map.Coords.GetLength(0); y++)
                {
                    Rectangle indexRect = new Rectangle(x * TileWidth, y * TileHeight, TileWidth, TileHeight);
                    int Index = Map.Coords[x,y];//Heres the Error!
                    Texture2D tex = Program.Tiles[Index];
                    Tile tile = new Tile(tex, indexRect);
                    spriteBatch.Draw(Program.Tiles[1], indexRect , Color.White);
                    tile.Draw(spriteBatch);




                }

            }






            spriteBatch.End();







            base.Draw(gameTime);
        }
    }
}

这是程序类......

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

namespace Platformer
{
#if WINDOWS || XBOX
    public static class Program
    {
        public static List<Texture2D> Tiles = new List<Texture2D>();
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {

            MainGame Game = new MainGame();
            Game.Run();

        }
    }
#endif
}

请帮忙!我不想听起来像我在这里,所以你可以调试我的代码,但我真的没有进一步...

1 个答案:

答案 0 :(得分:5)

我相信你的尺寸已经倒退了。它应该是

for (int x = 0; x < Map.Coords.GetLength(0); x++)
{
    for (int y = 0; y < Map.Coords.GetLength(1); y++)
    {
        int Index = Map.Coords[x,y]; 

或者这个:

for (int x = 0; x < Map.Coords.GetLength(1); x++)
{
    for (int y = 0; y < Map.Coords.GetLength(0); y++)
    {
        int Index = Map.Coords[y,x]; 
相关问题