【发布时间】:2014-05-21 23:59:54
【问题描述】:
我正在使用瓷砖地图,如果移动平台撞到瓷砖,它应该向另一个方向移动。我正在为 tilemap 和平台使用一个列表,这有助于保持整洁。
下图显示了平台,它一直在运动,当它与其中一个黑色圆圈相撞时,它应该改变方向并朝着相反的方向前进。
不幸的是,我遇到了一个问题,我需要找到正确的平台,这样它就会朝着另一个方向发展。我在主类中创建了列表,但不知道如何在主类之外的另一个类中调用列表。
问题:
我如何调用一个列表,该列表包含多个沿不同方向、不同位置的对象,并且是在主类中创建的,以便我可以知道平台是否与图块发生碰撞?
简单来说,如何让平台与瓷砖碰撞?
下面是瓦片地图使用和调用必须经过的过程:
为了制作瓦片地图,使用了两个类,Block 类和 Game1 类(主类)。
在 Block 类中,Texture、Position 和 BlockState(决定它会做什么)
public Block(Texture2D Texture, Vector2 Position, int BlockState)
{
this.Texture = Texture;
this.Position = Position;
this.BlockState = BlockState;
}
那么 Block 类将使用一个使用 Player 类的方法。
public Player BlockCollision(Player player)
{
Rectangle top = new Rectangle((int)Position.X + 5, (int)Position.Y - 10, Texture.Width - 10, 10);
Rectangle bottom = new Rectangle((int)Position.X + 5, (int)Position.Y + Texture.Height, Texture.Width - 10, 10);
Rectangle left = new Rectangle((int)Position.X - 10, (int)Position.Y + 5, 10, Texture.Height - 10);
Rectangle right = new Rectangle((int)Position.X + Texture.Width, (int)Position.Y + 5, 10, Texture.Height - 10);
if (BlockState == 1 || (BlockState == 2 && !player.goingUp))
{
if (top.Intersects(new Rectangle((int)player.Position.X, (int)player.Position.Y, player.Texture.Width, player.Texture.Height)))
{
if (player.Position.Y + player.Texture.Height > Position.Y && player.Position.Y + player.Texture.Height < Position.Y + Texture.Height / 2)
{
player.Position.Y = player.ground = Position.Y - player.Texture.Height;
player.Velocity.Y = 0;
player.isJumping = false;
player.Time = 0;
player.botCollision = true;
}
}
}
return player;
}
然后是 Draw 方法。
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Texture, new Rectangle((int)Position.X, (int)Position.Y, Texture.Width, Texture.Height), Color.White);
}
然后我们转到主类 Game1。
我为块和平台创建列表。
List<Block> Blocks;
List<MovingPlatform> Platforms;
我使用字符系统创建地图。
List<char[,]> Levels = new List<char[,]>();
public int tileWidth, tileHeight;
然后在 Initialize 方法中调用 Lists 并创建地图。
protected override void Initialize()
{
Blocks = new List<Block>();
Platforms = new List<MovingPlatform>();
char[,] Level1 = {{'.','.','#'},
{'.','.','#'},
{'.','.','#'}};
Levels.Add(Level1);
base.Initialize();
}
然后调用一个 void LoadLevel。
void LoadLevel(int level)
{
Blocks.Clear();
Platforms.Clear();
player.Position = Vector2.Zero;
tileWidth = Levels[level].GetLength(1);
tileHeight = Levels[level].GetLength(0);
Texture2D blockSpriteA = Content.Load<Texture2D>("blockA2");
for (int x = 0; x < tileWidth; x++)
{
for (int y = 0; y < tileHeight; y++)
{
//Background
Blocks.Add(new Block(background, new Vector2(x * 50, y * 50), 0));
//Impassable Blocks
if (Levels[level][y, x] == '#')
{
Blocks.Add(new Block(blockSpriteA, new Vector2(x * 50, y * 50), 1));
}
//Vertical Moving Platform
if (Levels[level][y, x] == '=')
{
Platforms.Add(new MovingPlatform(platform, new Vector2(x * 50, y * 50), 3.0f, true));
}
然后在更新中我调用列表。
foreach (Block b in Blocks)
{
player = b.BlockCollision(player);
}
foreach (MovingPlatform m in Platforms)
{
player = m.BlockCollision(player);
m.Update(gameTime);
}
Draw 方法终于调用了它们。
foreach (Block b in Blocks)
{
b.Draw(spriteBatch);
}
foreach (MovingPlatform m in Platforms)
{
m.Draw(spriteBatch);
}
【问题讨论】: