Be a Supporter!

Arraylist Collision Detection Xna

  • 378 Views
  • 5 Replies
New Topic Respond to this Topic
PandaexpressMan
PandaexpressMan
  • Member since: Apr. 10, 2011
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Arraylist Collision Detection Xna Jan. 27th, 2013 @ 06:44 PM Reply

So I have this object and when it hits a "bullet"(That is part of a list) I want the game to exit. When I try to detect the collision of the objects by using the rectangle.intersects method the game will exit randomly and the collision is not apparent. Please help, here is my code:

if (me.getRectangle().Intersects(enemy.getRectangle()))
this.Exit();

I just want to know if this would work or not?

PandaexpressMan
PandaexpressMan
  • Member since: Apr. 10, 2011
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to Arraylist Collision Detection Xna Jan. 27th, 2013 @ 09:00 PM Reply

foreach (Enemy enemy in enemies)
{
if (me.getRectangle().Intersects(enemy.getRectangle()))
this.Exit();
}

Sorry, I should probably post the whole loop :D

kiwi-kiwi
kiwi-kiwi
  • Member since: Mar. 6, 2009
  • Offline.
Forum Stats
Member
Level 09
Programmer
Response to Arraylist Collision Detection Xna Jan. 28th, 2013 @ 12:59 AM Reply

You initialized the rectangles wrong and now they're too big, I bet you're scaling the bullets in your draw code and leave the rectangle as is

PandaexpressMan
PandaexpressMan
  • Member since: Apr. 10, 2011
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to Arraylist Collision Detection Xna Jan. 28th, 2013 @ 05:27 PM Reply

Here is my constructor for my Enemy class:

public Enemy(Texture2D texture, Vector2 location,ContentManager content)
{
this.texture = texture;
this.location = location;

randX = random.Next(-2, 2);
if (randX == 0)
randX++;
randy = random.Next(-2, 2);
if (randy == 0)
randy++;

border = new Rectangle((int)location.X, (int)location.Y, texture.Width, texture.Height);

}

And then here is the update method that is called and is constantly updating them:

public void update()
{

location.X += randX;
location.Y += randy;

//updates the texture width & height
border.Width = texture.Width;
border.Height = texture.Height;

//updates the location
border.X = (int)location.X;
border.Y = (int)location.Y;

checkBoundsX();
checkBoundsY();

}

deckheadtottie
deckheadtottie
  • Member since: Oct. 21, 2003
  • Offline.
Forum Stats
Supporter
Level 58
Programmer
Response to Arraylist Collision Detection Xna Jan. 29th, 2013 @ 05:40 AM Reply

Please wrap your code in code tags.

<code> Code goes here. </code>


#coys

BBS Signature
PandaexpressMan
PandaexpressMan
  • Member since: Apr. 10, 2011
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to Arraylist Collision Detection Xna Jan. 29th, 2013 @ 11:05 PM Reply

At 1/29/13 05:40 AM, deckheadtottie wrote: Please wrap your code in code tags.

Code goes here.

I will be sure to next time. :D On the other hand, I figured it out. I was creating a new rectangle every time it the update method was called in the Character class(the Character collides with the enemy). So I moved that line of code into the constructor and then in the update method just kept updating the position of the sprite.