碰撞测试();不像预期的那样工作..只有一个盒子有碰撞......这段代码可能会伤害你的眼睛,它可能是做我想做的事情的坏方法
游戏1.cs
Texture2D boxTexture;
GameObject box, box1, box2;
List <GameObject> boxlist;
Random rand = new Random();
LoadContent()
boxTexture = Content.Load<Texture2D>("box");
for(int i = 0; i < boxlist.Count; i++)
boxlist[i] = new GameObject(boxTexture, new Vector2(rand.Next(100, 1200), rand.Next(500, 600)));
Update()
CollisionTest();
public void CollisionTest()
{
for (int i = 0; i < boxlist.Count; i++)
{
if (player.collisionRect.Intersects(boxlist[i].collisionRect))
{
ground.Y = boxlist[i].collisionRect.Y;
player.setPositionY(0);
}
else
{
ground.Y = 640;
}
}
}
Draw()
for (int i = 0; i < boxlist.Count; i++)
boxlist[i].Draw(spriteBatch);
游戏对象.cs
public Rectangle collisionRect
{
get
{
return new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);
}
}