我正在弄清楚XNA是如何工作的,我正在尝试使用基于磁贴的游戏。现在,当我注册tile时,我将它们存储在一个列表中,但是当类加载器调用列表时,它是空的。
The Tile Handler
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);
}
}
}
这会添加Tiles:
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返回正确的数字。
答案 0 :(得分:2)
您需要返回您在
中创建的Tile
对象
class CreateMap
{
public Tile createMap(int size, SpriteBatch spriteBatch, List<Texture2D> texture)
{
Tile tile = new Tile();
// do the work
return tile;
}
}
这需要返回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();
}
}