2015-01-21 3 views
-4

저는 XNA의 작동 방식을 파악하고 타일 기반 게임을 실험하고있었습니다. 이제 타일을 등록 할 때 목록에 저장하지만 클래스 로더가 목록을 호출하면 빈 상태가됩니다.목록이 갑자기 비어집니다.

타일 처리기

public class Tile 
{ 
    public class TileType 
    { 
     public int ID; 
     public Texture2D Texture; 
     public Rectangle Rectangle; 
    } 

    private List<Handler.Tile.TileType> tiles = new List<Handler.Tile.TileType>(); 

    public List<Handler.Tile.TileType> Tiles 
    { 
     get { return tiles; } 
    } 

    int TileNumber = 0; 

    Game1 game = new Game1(); 


    public void LoadTile(int ID, SpriteBatch spriteBatch) 
    { 
     spriteBatch.Draw(tiles[ID].Texture, tiles[ID].Rectangle, Color.White); 
    } 

    public void AddTile(Texture2D texture, Rectangle rectangle, SpriteBatch spriteBatch) 
    { 
     tiles.Add(new TileType { ID = TileNumber, Texture = texture, Rectangle = rectangle }); 
     TileNumber++; 
     Console.WriteLine("[Tile.cs] - Number of tiles in tiles: " + tiles.Count); 
     Console.WriteLine("[Tile.cs] - Added a tile, with ID of " + tiles[TileNumber-1].ID); 
    } 

    public void LoadMap(SpriteBatch spriteBatch) 
    { 
     foreach (TileType tile in tiles) 
     { 
      LoadTile(tile.ID, spriteBatch); 
      Console.WriteLine("Loaded Tile: " + tile.ID + "With Texture: " + tile.Texture); 
     } 
    } 
} 

이것은 타일 추가 :

class CreateMap 
{ 
    public void createMap(int size, SpriteBatch spriteBatch, List<Texture2D> texture) 
    { 
     Tile tile = new Tile(); 
     Random rnd = new Random(); 
     int air = 1; 
     switch(size) 
     { 
      case 1: 
       size = -64; 
       break; 
      case 2: 
       size = -128; 
       break; 
      case 3: 
       size = -256; 
       break; 
      default: 
       size = -64; 
       break; 
     } 
     for (int x = size; x <= (size * -1); x++) 
      for (int y = 0; y < 65; y++) 
      { 
       if (air <= 3) 
       { 
        tile.AddTile(texture[1], new Rectangle(x * 64, y * 64, 64, 64), spriteBatch); 
        air++; 
       } 
       else 
       { 
        switch (rnd.Next(0, 3)) 
        { 
         case 1: 
          tile.AddTile(texture[1], new Rectangle(x * 64, y * 64, 64, 64), spriteBatch); 
          Console.WriteLine("Added Tile with texture of Dirt"); 
          break; 
         case 2: 
          tile.AddTile(texture[0], new Rectangle(x * 64, y * 64, 64, 64), spriteBatch); 
          Console.WriteLine("Added Tile with texture of Sky"); 
          break; 
        } 
       } 
       Console.WriteLine("[ CreateMap.cs ] - Tile Number: " + tile.Tiles.Count); 
      } 
    } 
} 

그리고 내용은 Game1.cs

public class Game1 : Microsoft.Xna.Framework.Game 
{ 
    GraphicsDeviceManager graphics; 
    SpriteBatch spriteBatch; 
    Handler.Tile tile; 
    Handler.CreateMap mapHandler; 
    Handler.TextureHandler textureHandler; 

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

    protected override void Initialize() 
    { 
     tile = new Handler.Tile(); 
     mapHandler = new Handler.CreateMap(); 
     textureHandler = new Handler.TextureHandler(); 
     base.Initialize(); 
    } 

    protected override void LoadContent() 
    { 
     spriteBatch = new SpriteBatch(GraphicsDevice); 
     textureHandler.LoadTextures(Content); 
     mapHandler.createMap(1, spriteBatch, textureHandler.Textures); 
    } 

    protected override void UnloadContent() 
    { 
    } 

    protected override void Update(GameTime gameTime) 
    { 
     if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 
      this.Exit(); 
     Console.WriteLine("[Game1.cs/Update ] - Tiles in Update: " + tile.Tiles.Count); 

     base.Update(gameTime); 
    } 

    protected override void Draw(GameTime gameTime) 
    { 
     GraphicsDevice.Clear(Color.CornflowerBlue); 

     spriteBatch.Begin(); 
     tile.LoadMap(spriteBatch); 
     spriteBatch.End(); 

     base.Draw(gameTime); 
    } 
} 

(더 나은) 설명 : LoadMap()가 호출, Console.WriteLine()은 0을 반환하고 CreateMap은 올바른 숫자를 반환합니다.

+1

반환이 필요

class CreateMap { public Tile createMap(int size, SpriteBatch spriteBatch, List<Texture2D> texture) { Tile tile = new Tile(); // do the work return tile; } } 

에서 만든 Tile 객체를 반환해야 ..? – MethodMan

+0

예, 아무것도 찾지 못했습니다 : \ – Sqbika

+0

'CreateMap.createMap'는 타일을 실제로 추가 한'타일 '을 반환하지 않습니다. 'Draw'에서'LoadMap'을 호출 한'Tile'은 어떤 타일도 추가하지 않았습니다. –

답변

2

당신은 당신이 당신이 코드를 디버깅 시도 Game1

public class Game1 : Microsoft.Xna.Framework.Game 
{ 
    // ... 

    protected override void Initialize() 
    { 
     mapHandler = new Handler.CreateMap(); 
     tile = mapHandler.createMap(); 
     textureHandler = new Handler.TextureHandler(); 
     base.Initialize(); 
    } 
} 
+0

어떻게 할 수 있습니까? 타일이 Tile.tiles에 저장되므로 반환 값을 처리 할 필요가 없습니다. – Sqbika

+0

'CreateMap.createMap'을 참조하여'Tile'을 전달할 수 있습니다. –

관련 문제