Got a problem, with collision detection, I need to detect collision between bullets and meteors using nested loop for this, but it isn't working.
}
}
check();
LoadMeteors();
base.Update(gameTime);
}
public void LoadMeteors()
{
int randY = random.Next(100, 400);
if (spawn >= 0.5)
{
spawn = 0;
meteors.Add(new Meteor(Content.Load<Texture2D>("Backgrounds/meteori"), new Vector2(800, randY), new Rectangle(0,0,50,50)));
}
}
public void SpawnBullet()
{
bullets.Add(new Bullet(Content.Load<Texture2D>("Backgrounds/pula"), new Vector2(rocket.rectangle.X + 20, rocket.rectangle.Y + 20), new Rectangle(0, 0, 10, 5)));
soundEffect.Play();
}
public void check()
{
for (k = 0; k < bullets.Count; k++)
{
for (i = 0; i < meteors.Count; i++)
{
if (bullets[k].rectangle.Intersects(meteors[i].rectangle))
{
meteors[i].isVisible = false;
}
}
}
}
that is my meteor class:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Rocket
{
class Meteor
{
public Texture2D texture;
public Rectangle rectangle;
public Vector2 position;
public Vector2 velocity;
public bool isVisible = true;
Random random = new Random();
int randX, randY;
public Meteor(Texture2D newTexture, Vector2 newPosition, Rectangle newRectangle)
{
texture = newTexture;
position = newPosition;
rectangle = newRectangle;
randY = random.Next(-4, 4);
randX = random.Next(-4,-1);
velocity = new Vector2(7, 0);
}
public void Update(GraphicsDevice graphics)
{
position -= velocity;
if (position.X < 0 - texture.Width)
isVisible = false;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, position, rectangle, Color.White);
}
}
}
and that is my bullet class:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Rocket
{
class Bullet
{
public Texture2D texture;
public Rectangle rectangle;
public Vector2 position;
public Vector2 velocity;
public bool isVisible = true;
public Bullet(Texture2D newTexture, Vector2 newPosition, Rectangle newRectangle)
{
texture = newTexture;
position = newPosition;
rectangle = newRectangle;
velocity = new Vector2(10, 0);
}
public void Update(GraphicsDevice graphics)
{
position += velocity;
if (position.X < 0 - texture.Width)
isVisible = false;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, position, rectangle, Color.Aqua);
}
}
so, I call function Check(): in update method, than in that function i check for collision, but it isn't detecting collision, also meteor and bullet classes, both have rectangle, I don't get any errors and it seems to be right, thanks for any help!