저는 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은 올바른 숫자를 반환합니다.
반환이 필요
에서 만든
Tile
객체를 반환해야 ..? – MethodMan예, 아무것도 찾지 못했습니다 : \ – Sqbika
'CreateMap.createMap'는 타일을 실제로 추가 한'타일 '을 반환하지 않습니다. 'Draw'에서'LoadMap'을 호출 한'Tile'은 어떤 타일도 추가하지 않았습니다. –