2013-07-24 116 views
1

我刚刚决定,我应该尝试做一个基于瓷砖的“游戏”。 我遵循了Nick Gravelyn和​​YouTube上的ouyyu91的教程,无论我做什么, 一直告诉我Map.Coords的索引超出范围。 (我希望你能读到的东西出这个代码,我并没有真正在意的架构)为什么指数超出范围?

Game类:

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

你从哪里得到这个错误吗? – SLaks

+0

@SLaks - 上面写着'int Index = Map.Coords [x,y]; //这个错误!'可能 – mcmonkey4eva

+0

sry,提示错误的地方是“Map.Coords [y,x]; “ – calcyss

回答

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

是的,我在我的项目中误解了它的“y,x”,但它仍然一直给我提供那个错误 – calcyss

+0

@DanielWanner那么,在这种情况下,你必须有一些其他的代码没有向我们展示,这正在改变'Coords'。 –

+0

好吧,因为这是修改或甚至使用Coords的唯一代码,我猜这里有一些令人毛骨悚然的小错误,我只是没有找到... :( – calcyss