【发布时间】:2014-03-25 13:11:51
【问题描述】:
使用基于图块的系统,我需要将平台块与其他块分开,然后让它们以恒定的速度上下移动。
如何使标有“=”(垂直移动平台)的图块向上或向下移动,直到碰到“#”(墙)或端点“+”(垂直移动平台)移动区域)?
有一个跟随玩家的相机类,一个控制移动的玩家类和一个控制方块碰撞的方块类。
列出已存储的块:
List<Block> Blocks;
列表是存储的级别:
List<char[,]> Levels = new List<char[,]>();
这里是创建关卡的地方(测试地图):
protected override void Initialize()
{
Blocks = new List<Block>();
char[,] Level2 = {{'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.','.','.','.','.','+','.'},
{'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.','.','.','#','.','.','.'},
{'.','.','.','.','.','.','.','-','-','-','.','.','#','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.','.','.','#','.','.','.'},
{'.','.','.','-','-','-','.','.','.','.','.','.','#','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.','.','.','#','.','.','.'},
{'#','#','.','.','.','.','.','.','.','P','.','.','#','.','=','.'},
{'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'}};
Levels.Add(Level2);
base.Initialize();
}
这里是 void LoadLevel,它将赋予关卡的意义(当玩家完成关卡并在 LoadContent 方法中调用):
void LoadLevel(int level)
{
Blocks.Clear();
player.Position = Vector2.Zero;
tileWidth = Levels[level].GetLength(1);
tileHeight = Levels[level].GetLength(0);
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));
}
//Blocks that are only passable if going up them
if (Levels[level][y, x] == '-')
{
Blocks.Add(new Block(blockSpriteB, new Vector2(x * 50, y * 50), 2));
}
//Vertical Moving Platform Movement Area
if (Levels[level][y, x] == '+')
{
Blocks.Add(new Block(movingArea, new Vector2(x * 50, y * 50), 3));
}
//Vertical Moving Platform
if (Levels[level][y, x] == '=')
{
Blocks.Add(new Block(movingPlatform, new Vector2((x * 50), (y * 50) + movingPlatform.Height), 4));
}
//PlayerSpawn
if (Levels[level][y, x] == 'P' && player.Position == Vector2.Zero)
{
player.Position = new Vector2(x * 50, (y + 1) * 50 - player.Texture.Height);
player.Velocity = new Vector2(0, 0);
player.initialVelocity = 0;
player.Time = 0;
player.isJumping = false;
}
else if (Levels[level][y, x] == 'P' && player.Position != Vector2.Zero)
{
throw new Exception("Only one 'P' is needed for each level");
}
}
}
if (player.Position == Vector2.Zero)
{
throw new Exception("Player Position needs to be set with 'P'");
}
}
更新方法:
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
HandleInput(Keyboard.GetState());
player.Update(gameTime);
Time += (float)gameTime.ElapsedGameTime.TotalSeconds;
foreach (Block b in Blocks)
{
player = b.BlockCollision(player);
}
camera.thing(player);
prevKB = Keyboard.GetState();
base.Update(gameTime);
}
绘制方法:
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.White);
// TODO: Add your drawing code here
spriteBatch.Begin(SpriteSortMode.Texture, BlendState.AlphaBlend, null, null, null, null, camera.Transform());
//spriteBatch.Begin();
foreach (Block b in Blocks)
{
b.Draw(spriteBatch);
}
spriteBatch.End();
base.Draw(gameTime);
}
【问题讨论】:
-
为您想要移动的对象创建一个类并为其添加碰撞。
-
但是如何让它移动呢?如何调出该位置的所有块并使其向上或向下移动?