-1
\$\begingroup\$

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);
\$\endgroup\$
2
  • \$\begingroup\$ It should work, but you can also do this Vector2 fuellevelTextPosition = fuellevelPosition + new Vector(30, 0); And what does it mit it didnt work? \$\endgroup\$
    – Kikaimaru
    Jul 25, 2012 at 10:19
  • \$\begingroup\$ you code is right.. what's the matter? \$\endgroup\$
    – Blau
    Jul 25, 2012 at 10:40

1 Answer 1

3
\$\begingroup\$

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);
}
\$\endgroup\$
4
  • \$\begingroup\$ 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 ?? \$\endgroup\$
    – 3D-kreativ
    Jul 25, 2012 at 10:26
  • 1
    \$\begingroup\$ You are going to have to show us more of your code. \$\endgroup\$ Jul 25, 2012 at 10:28
  • \$\begingroup\$ I edited my answer to suit your update. \$\endgroup\$ Jul 25, 2012 at 10:37
  • 1
    \$\begingroup\$ You're right! I didn't thought of that! Thanks! \$\endgroup\$
    – 3D-kreativ
    Jul 25, 2012 at 10:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .