Okay, so I have been searching for hours without any results and really hope that you could help me. I'm having some trouble getting the intersect to work. Nothing happends when my classes collide which is probably me being blind but also really new to XNA. I have created a Shoot-Em-Up game and I want the game to end as the player controlled space shuttle collides with an enemy.
Using classes; Game1.cs, Player.cs and Enemy.cs. Notice that I have only pasted the info I think is necessary to figure this out.
Game1.cs
protected override void LoadContent()
{
shuttle = new Player(Content.Load<Texture2D>(@"spaceship"), new Vector2(300, 400), new Vector2(24, 24), spriteBatch);
}
protected override void Update(GameTime gameTime)
{
if (random.Next(0, 70) < droprate)
{
int posX = random.Next(20, 580);
enemyList.Add(new Enemy(Content.Load<Texture2D>(@"enemy1"), new Vector2(posX, -50), new Vector2(24, 24), spriteBatch));
droprate += 0.10;
}
foreach (Enemy e in enemyList)
{
if (shuttle.rectangle.Intersects(e.rectangle))
Exit();
}
}
Player.cs
public Rectangle rectangle;
public Player(Texture2D texture, Vector2 initialPos, Vector2 origin, SpriteBatch spriteBatch) : base(texture, initialPos, origin, spriteBatch)
{
this.rectangle = new Rectangle(Convert.ToInt32(initialPos.X), Convert.ToInt32(initialPos.Y), 48, 48);
}
public override void Update(GameTime gameTime)
{
KeyboardState keyboardState = Keyboard.GetState();
if (keyboardState.IsKeyDown(Keys.Right))
{
initialPos.X += 9;
}
if (keyboardState.IsKeyDown(Keys.Left))
{
initialPos.X -= 9;
}
if (keyboardState.IsKeyDown(Keys.Up))
{
initialPos.Y -= 5;
}
if (keyboardState.IsKeyDown(Keys.Down))
{
initialPos.Y += 5;
}
initialPos.X = MathHelper.Clamp(initialPos.X, 18, screenWidth - 18);
initialPos.Y = MathHelper.Clamp(initialPos.Y, 24, screenHeight - 24);
base.Update(gameTime);
}
Enemy.cs
public Rectangle rectangle;
public Enemy(Texture2D texture, Vector2 initialPos, Vector2 origin, SpriteBatch spriteBatch) : base(texture, initialPos, origin, spriteBatch)
{
this.rectangle = new Rectangle(Convert.ToInt32(initialPos.X), Convert.ToInt32(initialPos.Y), 48, 48);
}
public override void Update(GameTime gameTime)
{
initialPos.Y += 3.5f;
if (initialPos.X < 300)
initialPos.X += 0.41f;
if (initialPos.X > 300)
initialPos.X -= 0.41f;
}