Sign in to follow this  

Help with simple collision

This topic is 2574 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Basically im trying to do a rectangle collision so if rectangle intersects with blah blah then do this, but im trying to do this between two separate classes so when I run the code it is saying that it does not exist in current context because im guessing that the rectangle im trying to intersect it with is in the other class therefore not knowing where it is, is there a way I can get it to detect the rectangle in the other class?

Share this post


Link to post
Share on other sites
I believe Vilem was giving you examples on how to structure your program

Quote:
is there a way I can get it to detect the rectangle in the other class?


You might as well pass them as Parameters like Vilem suggested? Could you post a stripped down version of your code with all the essentials?

Share this post


Link to post
Share on other sites
Well this is the bit im having the trouble with. Basically when I run it, it is saying the other rectangle in the rectangle does not exist in current context. I've tried putting them both in the same class but the way the rest of my code is laid out I need them as two separate classes. Was just wondering if there is a way I can do this or do I have to restructure the rest of my code?

public class Warrior
{

public void Update(GameTime gameTime)
{

Rectangle warriorRectangle = new Rectangle((int)warriorPosition.X, (int)warriorPosition.Y, warriorTexture.Width, warriorTexture.Height);

warriorHit = false;

if (warriorRectangle.Intersects(enemyWarriorRectangle))
{
warriorHit = true;
}

if (warriorHit)
{
warriorPosition = new Vector2 (0,0);
}
}
}


public class enemyWarrior
{

public void Update(GameTime gameTime)
{

Rectangle enemyWarriorRectangle = new Rectangle((int)enemyWarriorPosition.X, (int)enemyWarriorPosition.Y, enemyWarriorTexture.Width, enemyWarriorTexture.Height);

enemyWarriorHit = false;

if (enemyWarriorRectangle.Intersects(warriorRectangle))
{
enemyWarriorHit = true;
}

if (enemyWarriorHit)
{
enemyWarriorPosition = new Vector2(0, 200);
}
}
}

[Edited by - maximo1491 on November 30, 2010 5:11:25 AM]

Share this post


Link to post
Share on other sites

This topic is 2574 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this