1

我最近才决定我应该尝试做一个基于瓷砖的小“游戏”。我按照 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
}

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

4

1 回答 1

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]; 
于 2013-07-24T23:04:39.350 回答