C# 移动列表中的对象,与玩家发生碰撞
我最近问了一个关于它们应该如何与瓷砖地图碰撞的问题,得到了一个由于时间限制而无法在游戏中实现的答案,我即兴创作并实现了它。但现在我要讨论另一个在编码时出现的问题,那就是我的平台突然停止与播放器碰撞。前一秒他们可以,下一秒他们没有 我看了一下,发现平台与玩家的交互方式没有什么不同,但有些平台就是不起作用。我为这些事情的发生而感到心烦意乱 是最后一篇关于它们如何与瓷砖碰撞的文章 是指向项目的链接,如果您只想查看这堆垃圾代码的话 我使用三个类来移动平台,主类Game1、MovingPlatform和Block。Game1运行所有这些,MovingPlatform帮助列表并使平台勾选,块帮助与平铺碰撞 问题 我做错了什么使平台不发生碰撞 我将从MovingPlatform类开始:C# 移动列表中的对象,与玩家发生碰撞,c#,list,xna,collision,C#,List,Xna,Collision,我最近问了一个关于它们应该如何与瓷砖地图碰撞的问题,得到了一个由于时间限制而无法在游戏中实现的答案,我即兴创作并实现了它。但现在我要讨论另一个在编码时出现的问题,那就是我的平台突然停止与播放器碰撞。前一秒他们可以,下一秒他们没有 我看了一下,发现平台与玩家的交互方式没有什么不同,但有些平台就是不起作用。我为这些事情的发生而感到心烦意乱 是最后一篇关于它们如何与瓷砖碰撞的文章 是指向项目的链接,如果您只想查看这堆垃圾代码的话 我使用三个类来移动平台,主类Game1、MovingPlatform和B
public class MovingPlatform
{
Game1 game1;
public Texture2D Texture;
public Vector2 Velocity;
public Vector2 Position;
public Rectangle Size;
public Rectangle World;
public float Speed;
public bool Up;
public bool Thing;
float Time;
public MovingPlatform(Texture2D Texture, Vector2 Position,float Speed, bool Up)
{
this.Texture = Texture;
this.Position = Position;
this.Speed = Speed;
this.Up = Up;
Thing = true;
Velocity = Vector2.Zero;
Size = new Rectangle((int)Position.X, (int)Position.Y, 50, 25);
}
public void Initialize(Game1 game1)
{
this.game1 = game1;
}
public Player BlockCollision(Player player)
{
Rectangle top = new Rectangle((int)Position.X + 5, (int)Position.Y - 10, Size.Width - 10, 10);
Rectangle bottom = new Rectangle((int)Position.X + 5, (int)Position.Y + Size.Height, Size.Width - 10, 10);
Rectangle left = new Rectangle((int)Position.X - 10, (int)Position.Y + 5, 10, Size.Height - 10);
Rectangle right = new Rectangle((int)Position.X + Size.Width, (int)Position.Y + 5, 10, Size.Height - 10);
if (!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;
}
}
else if (player.isJumping == false && player.ground == Position.Y - player.Texture.Height)
{
player.isJumping = true;
player.initialVelocity = 0;
}
}
return player;
}
public void Update(GameTime gameTime)
{
Time += (float)gameTime.ElapsedGameTime.TotalSeconds;
World = new Rectangle(0, 0, 0, 0);
if (Up == true)
{
if (Thing == true)
{
Position.Y -= 2;
}
else if (Thing == false)
{
Position.Y += 2;
}
}
if (Up == false)
{
if (Thing == true)
{
Position.X -= 2;
}
else if (Thing == false)
{
Position.X += 2;
}
}
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Texture, new Rectangle((int)Position.X, (int)Position.Y, Size.Width, Size.Height), Color.White);
}
}
}
公共类移动平台
{
游戏1游戏1;
公共纹理2D纹理;
公共矢量2速度;
公共向量2位置;
公共矩形尺寸;
公共世界;
公众浮标速度;
公开募捐;
公共事务;
浮动时间;
公共移动平台(纹理2D纹理、矢量2位置、浮动速度、向上移动)
{
这个。纹理=纹理;
这个位置=位置;
这个。速度=速度;
这个。向上=向上;
事物=真实;
速度=矢量2.0;
大小=新矩形((int)位置.X,(int)位置.Y,50,25);
}
公共无效初始化(Game1 Game1)
{
this.game1=game1;
}
公共玩家区块冲突(玩家)
{
矩形顶部=新矩形((int)位置.X+5,(int)位置.Y-10,大小.宽度-10,10);
矩形底部=新矩形((int)位置.X+5,(int)位置.Y+Size.Height,Size.Width-10,10);
矩形左=新矩形((int)位置.X-10,(int)位置.Y+5,10,大小.高度-10);
右矩形=新矩形((int)位置.X+大小.宽度,(int)位置.Y+5,10,大小.高度-10);
如果(!player.goingUp)
{
if(顶部相交(新矩形((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
区块:
public class Block
{
Game1 game1;
public Texture2D Texture;
public Vector2 Position;
public int BlockState;
KeyboardState prevKB;
KeyboardState kbState;
public static bool NextLevel = false;
Rectangle Contain;
public Block(Texture2D Texture, Vector2 Position, int BlockState)
{
this.Texture = Texture;
this.Position = Position;
this.BlockState = BlockState;
Contain = new Rectangle((int)Position.X, (int)Position.Y, 50, 50);
}
public void Initialize(Game1 game1)
{
this.game1 = new Game1();
}
public Player BlockCollision(Player player, GameTime gameTime, Game1 game1)
{
this.game1 = game1;
kbState = Keyboard.GetState();
Rectangle BlockRectangle = new Rectangle((int)Position.X, (int)Position.Y, Texture.Width, Texture.Height);
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 > 0)
{
for (int i = 0; i < game1.Platforms.Count; i++)
{
if (top.Intersects(new Rectangle((int)game1.Platforms[i].Position.X, (int)game1.Platforms[i].Position.Y, game1.Platforms[i].Texture.Width, game1.Platforms[i].Texture.Height)))
{
game1.Platforms[i].Thing = true;
}
if (bottom.Intersects(new Rectangle((int)game1.Platforms[i].Position.X, (int)game1.Platforms[i].Position.Y, game1.Platforms[i].Texture.Width, game1.Platforms[i].Texture.Height)))
{
game1.Platforms[i].Thing = false;
}
if (left.Intersects(new Rectangle((int)game1.Platforms[i].Position.X, (int)game1.Platforms[i].Position.Y, game1.Platforms[i].Texture.Width, game1.Platforms[i].Texture.Height)))
{
game1.Platforms[i].Thing = false;
}
if (right.Intersects(new Rectangle((int)game1.Platforms[i].Position.X, (int)game1.Platforms[i].Position.Y, game1.Platforms[i].Texture.Width, game1.Platforms[i].Texture.Height)))
{
game1.Platforms[i].Thing = false;
}
}
}
prevKB = Keyboard.GetState();
return player;
}
公共类块
{
游戏1游戏1;
公共纹理2D纹理;
公共向量2位置;
公共国家;
键盘状态,KB;
键盘状态;
公共静态bool NextLevel=false;
矩形包含;
公共块(Texture2D纹理,矢量2位置,int BlockState)
{
这个。纹理=纹理;
这个位置=位置;
this.BlockState=BlockState;
包含=新矩形((int)位置.X,(int)位置.Y,50,50);
}
公共无效初始化(Game1 Game1)
{
this.game1=新的game1();
}
公共玩家区块冲突(玩家、游戏时间、游戏1游戏1)
{
this.game1=game1;
kbState=Keyboard.GetState();
矩形块矩形=新矩形((int)位置.X,(int)位置.Y,纹理.宽度,纹理.高度);
矩形顶部=新矩形((int)位置.X+5,(int)位置.Y-10,纹理.Width-10,10);
矩形底部=新矩形((int)位置.X+5,(int)位置.Y+纹理.高度,纹理.宽度-10,10);
矩形左=新矩形((int)位置.X-10,(int)位置.Y+5,10,纹理.Height-10);
右矩形=新矩形((int)位置.X+纹理.Width,(int)位置.Y+5,10,纹理.Height-10);
如果(块状态>0)
{
对于(int i=0;i public List<Block> Blocks;
public List<MovingPlatform> Platforms;
protected override void Initialize()
{
Blocks = new List<Block>();
Platforms = new List<MovingPlatform>();
base.Initialize();
}
protected override void LoadContent()
{
movingPlatform = new MovingPlatform(ballSprite, Vector2.Zero, 3.0f, true);
movingPlatform.Initialize(this);
}
void LoadLevel(int level)
{
Blocks.Clear();
Platforms.Clear();
Texture2D platform = Content.Load<Texture2D>("Platform");
for (int x = 0; x < tileWidth; x++)
{
for (int y = 0; y < tileHeight; y++)
{
//Platform Stoper
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] == '=')
{
Platforms.Add(new MovingPlatform(platform, new Vector2(x * 50, y * 50), 3.0f, true));
}
}
}
}
protected override void Update(GameTime gameTime)
{
foreach (Block b in Blocks)
{
player = b.BlockCollision(player, gameTime, this);
}
foreach (MovingPlatform m in Platforms)
{
player = m.BlockCollision(player);
m.Update(gameTime);
}
}
protected override void Draw(GameTime gameTime)
{
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullCounterClockwise, null, camera.Transform());
foreach (Block b in Blocks)
{
b.Draw(spriteBatch);
}
foreach (MovingPlatform m in Platforms)
{
m.Draw(spriteBatch);
}
spriteBatch.End();
base.Draw(gameTime);
}
}