Hvid Hat wrote:
class Sprite
{
private Vector2 position;
public Vector2 Position
{
get { return position; }
set { position = value; }
}
...
}
class Vehicle : Sprite
{
public void Update()
{
Position.X += velocity.X; // <--- Cannot modify the return value
}
}
How to fix? And yes, I'm not an experienced C#-programmer :-)
This code isn't trying to do what you think it's trying to do...
Vector2 is a struct, so your call in Update first *copies* Position onto
the stack, adds velocity.X to the new Vector2's 'X' and then throws it
away. The original value was never modified.
The C# compiler catches your mistake in this case (phew!). You instead
need to do:
Position = new Vector2(Position.X+velocity.X, Position.Y);
The difference between value and reference types is fundamental in C# -
the C# specification describes that difference immediately after "hello,
world"
See section 8.2 of the C# specification for more information:
http://www.ecma-international.org/pu...T/Ecma-334.pdf
Alun Harford