Sign in to follow this  

Ok I know im doing something wrong.... and its probably something simple

This topic is 2575 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

Ok so im trying to get two objects to collide with each other and for now just to move them to a different position so that I know that the collision is working I can sort the rest out after the collisions are confirmed. Im getting 2 CS0103 error - "The name 'enemyWarriorRectangle' does not exist in current context" and "The name 'warriorRectangle' does not exist in current context". Basically I just want to know how I can fix this problem and any help is much appreciated. This is not the entire code but im assuming the issue is coming from the classes themselves rather than the actual game although if I am mistaken I can post the rest of the code.

public class Warrior
{

public Vector2 warriorPosition = new Vector2(250, 500);
public Vector2 warriorSpeed = new Vector2(1f, 0f);
public static Texture2D warriorTexture;

public bool warriorExists = true;
public bool warriorHit;


public Warrior()
{

}

public void Update(GameTime gameTime)
{
warriorPosition.X += warriorSpeed.X;

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 void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Begin();
spriteBatch.Draw(warriorTexture, warriorPosition, null, Color.Blue, 0,new Vector2(0, 0), 0.3f, SpriteEffects.None,0);
spriteBatch.End();

}
}

public class enemyWarrior
{

public Vector2 enemyWarriorPosition = new Vector2(1250, 500);
public Vector2 enemyWarriorSpeed = new Vector2(-1f, 0f);
public static Texture2D enemyWarriorTexture;

public bool enemyWarriorExists = true;
public bool enemyWarriorHit;

public enemyWarrior()
{
}

public void Update(GameTime gameTime)
{
enemyWarriorPosition.X += enemyWarriorSpeed.X;

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);
}
}

public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Begin();
spriteBatch.Draw(enemyWarriorTexture, enemyWarriorPosition, null, Color.Red, 0, new Vector2(0, 0), 0.3f, SpriteEffects.FlipHorizontally, 0);
spriteBatch.End();
}
}

Share this post


Link to post
Share on other sites
I'm guessing those are all those functions are member functions of either Warrior or EnemyWarrior, correct?

You need to specify an object whose member variables you are accessing. The compiler will assume that it's either a global variable, a function specific variable or a "this" variable if you don't specify anything infront of it.

My question would be, why do you even have 2 nearly identical Classes?
Just delete the whole enemyWarrior class and while Initializing a new Warrior object, let the user choose via a bool or something if it's an enemy or not.

Then during your collision routine, add a pointer to the second object so you can test it.

Share this post


Link to post
Share on other sites
Ok thanks I really don't know why I was using two different classes that, yeah, were practically the same, merged them both together and getting the collisions working so thanks for your help.

Share this post


Link to post
Share on other sites

This topic is 2575 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