2013-07-24 114 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