Pass by Value strikes again!!
player.Vector.X += player.Speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
does not work.
Vector2 v = player.Vector;
v.X -= player.Speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
player.Vector = v;
fixes this problem.
This is explained here: Can't modify XNA Vector components
The answer was very well explained and works just fine, but it has been 4 years since it was posted. My question is, since its been 4 years, is there a better way to fix this problem now?
I have about 100 lines of this I need to fix, and was hoping there was some sort of shortcut by now.
There is probably never going to be a "shortcut" for this since it's a feature of the fundamental difference between value types and reference types.
You can't directly modify a Vector
's X, Y, Z, etc properties in XNA. What you can try is creating a new Vector2
based off the player
's like this. This would reduce your 3 lines of code to 1 (if that's what your going for):
player.Vector = New Vector2(player.Vector.X - player.Speed *
(float)gameTime.ElapsedGameTime.TotalSeconds, player.Vector.Y);
This should do exactly what the "fix" did, except in one line. The same idea would apply to Vector3
or Vector4
, and if you wanted to modify more than one property of the vector.