Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

If I have a Vector2 variable, like this:

Vector2 fuellevelPosition = new Vector2(50, 750);

and I want to use that position for another object and also add 30 pixel to the X value for the position of the new object, how do I do that?

This didn't work:

Vector2 fuellevelTextPosition = new Vector2(fuellevelPosition.X + 30, fuellevelPosition.Y);

EDIT:

The code I use in the beginning of Game1.cs

 Vector2 fuellevelPosition = new Vector2(50, 750);
 Vector2 fuellevelTextPosition = new Vector2(fuellevelPosition.X + 30, fuellevelPosition.Y);
share|improve this question
    
It should work, but you can also do this Vector2 fuellevelTextPosition = fuellevelPosition + new Vector(30, 0); And what does it mit it didnt work? –  Kikaimaru Jul 25 '12 at 10:19
    
you code is right.. what's the matter? –  Blau Jul 25 '12 at 10:40

1 Answer 1

up vote 3 down vote accepted

Try something like this.

Vector2 fuellevelPosition = new Vector2(50, 750);
Vector2 fuellevelTextPosition = fuellevelPosition + new Vector2(30, 0);

Why didn't your current code work? It should.

Edit Based On Your Edit

You can't use fuellevelPosition before it's initialized. Assuming that those two lines of code are inside the Game1 class, just assign them for now, like so.

Vector2 fuellevelPosition;
Vector2 fuellevelTextPosition;

Then, in the constructor function for Game1 set the variables, like so.

public Game1()
{
    fuellevelPosition = new Vector2(50, 750);
    fuellevelTextPosition = fuellevelPosition + new Vector2(30, 0);
}
share|improve this answer
    
I get a red line below the fuellevelPosition and the error message is: A field initializer cannot reference the non-static field, method, or property ?? –  3D-kreativ Jul 25 '12 at 10:26
1  
You are going to have to show us more of your code. –  Austin Brunkhorst Jul 25 '12 at 10:28
    
I edited my answer to suit your update. –  Austin Brunkhorst Jul 25 '12 at 10:37
1  
You're right! I didn't thought of that! Thanks! –  3D-kreativ Jul 25 '12 at 10:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.