C# 带移动平台的平铺系统
使用基于瓷砖的系统,我需要将平台块与其余部分分开,然后允许它们以恒定速度上下移动 如何使标记为“=”(垂直移动平台)的平铺向上或向下移动,直到它碰到“#”(墙)或端点“+”(垂直移动平台移动区域)? 有一个跟随播放器的摄影机类、一个控制移动的播放器类和一个控制块碰撞的块类 存储块的列表:C# 带移动平台的平铺系统,c#,list,xna,2d,tile,C#,List,Xna,2d,Tile,使用基于瓷砖的系统,我需要将平台块与其余部分分开,然后允许它们以恒定速度上下移动 如何使标记为“=”(垂直移动平台)的平铺向上或向下移动,直到它碰到“#”(墙)或端点“+”(垂直移动平台移动区域)? 有一个跟随播放器的摄影机类、一个控制移动的播放器类和一个控制块碰撞的块类 存储块的列表: List<Block> Blocks; List<char[,]> Levels = new List<char[,]>(); 绘制方法: protected o
List<Block> Blocks;
List<char[,]> Levels = new List<char[,]>();
绘制方法:
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);
}
- 创建一个类似于
(类似于Block
)的类MovingBlock
- 在类中添加更新方法,使块上下移动
- 在LoadLevel中使用该类
if(Levels[level][y,x]='='=')
{ 移动块。添加(新移动块)(移动平台, 新矢量2((x*50),(y*50)+移动平台高度) , 4)); }
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);
}