【发布时间】:2015-07-16 14:39:27
【问题描述】:
我正在开发一个简单的小游戏,只是为了在设计过程中让我的脚湿透。我在尝试对列表中的项目进行碰撞检测时遇到了问题。我有一个敌人等级,一次会产生三个敌人,然后这些船每艘都有能力一次发射三颗子弹。我遇到的问题是我的碰撞检测正在跟踪船,而不是每个单独的子弹,即使碰撞矩形正在获取子弹位置。在拍摄子弹方法中,我分配了一个 Vector2 来复制子弹位置(很确定我必须通过传入索引来做一些事情,但我不知道从哪里开始)。任何帮助/指导将不胜感激!
SpriteManager.cs
for (int j = 0; j < enemies.Count; j++)
{
Enemies e = enemies[j];
//Update Sprite
e.Update(Game.GraphicsDevice, gameTime);
//Check for Collision
if (e.collisionRect.Intersects(player.collisionRect))
{
lives -= 1;
}
}
Enemies.cs
public void ShootBullets()
{
Bullets newBullet = new Bullets(bulletTexture);
newBullet.velocity.X = velocity.X - 3f;
newBullet.position = new Vector2(position.X + newBullet.velocity.X,
position.Y + (texture.Height / 2) - (bulletTexture.Height / 2));
//Test
bulletPos = newBullet.position;
newBullet.isVisible = true;
if (bullets.Count() < 3)
bullets.Add(newBullet);
}
public Rectangle collisionRect
{
get
{
return new Rectangle(
(int)bulletPos.X + collisionOffset,
(int)bulletPos.Y + collisionOffset,
bulletTexture.Width - (collisionOffset * 2),
bulletTexture.Height - (collisionOffset * 2));
}
}
编辑以添加子弹类:
public class Bullets
{
// Variables
public Texture2D texture;
public Vector2 position;
public Vector2 velocity;
public bool isVisible;
//public Rectangle boundingBox;
//public Vector2 origin;
//public float speed;
// Methods
// Constructor
public Bullets(Texture2D newTexture)
{
// speed = 10;
texture = newTexture;
isVisible = false;
}
// Draw
public void draw(SpriteBatch spritebatch)
{
spritebatch.Draw(texture, position, Color.White);
}
}
}
【问题讨论】:
标签: c# list xna collision-detection xna-4.0