gpt4 book ai didi

c# - 为什么索引超出范围?

转载 作者:太空宇宙 更新时间:2023-11-03 20:14:26 25 4
gpt4 key购买 nike

我最近决定尝试做一个基于图 block 的小“游戏”。我遵循了 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
}

求助!我不想听起来好像我只是在这里,所以你可以调试我的代码,但我真的没有得到任何进一步...

最佳答案

我相信您的维度倒退了。应该是

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];

关于c# - 为什么索引超出范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17846244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com