tags:

views:

132

answers:

2

I have a class called Sprite, and ballSprite is an instance of that class. Sprite has a Vector2 property called Position.

I'm trying to increment the Vector's X component like so:

ballSprite.Position.X++;

but it causes this error:

Cannot modify the return value of 'WindowsGame1.Sprite.Position' because it is not a variable

Is it not possible to set components like this? The tooltip for the X and Y fields says "Get or set ..." so I can't see why this isn't working.

+3  A: 

The problem is that ballSprite.Position returns a struct, so when you access it, it creates a copy of it due to the value semantics. ++ attempts to modify that value, but it'll be modifying a temporary copy - not the actual struct stored in your Sprite.

You have to take the value from reading the Position and put it into a local variable, change that local variable, and then assign the local variable back into Position, or use some other, similar way of doing it (maybe hiding it as some IncrementX method).

Vector2D v;
v = ballSprite.Position;
v.X++;
ballSprite.Position = v;

Another, more generic solution, might be to add another Vector2 to your Position. The + operator is overloaded, so you could create a new vector with the change you want, and then add that to the vector instead of updating the indiviudal components one at a time.

Michael Madsen
ballSprite.Position.X = ballSprite.Position.X + 1; doesn't work either - same error. :( Isn't that exactly the same as ballSprite.Position.X++ anyway?
Matt H
Sorry, I wasn't thinking straight there. I've fixed it.
Michael Madsen
Okay, looks like it's solved. This issue is kinda annoying. Thanks. I'll probably just add two vectors. :)
Matt H
+1  A: 

You can do

 Position = new Vector2(Position.X + 1, Position.Y );

or

 Position += new Vector2( 1.0f, 0.0f );

or

 Position.X += 1.0f;
Maynza
So is the summary of the X and Y fields a bug? // // Summary: // Gets or sets the y-component of the vector. Either way, thanks!
Matt H
Nope, I was mistaken, you just need to make sure you are adding a float with the f at the end.
Maynza
ballSprite.Position.X += 0.1f; That still gives me the same error. Besides, if the error was caused by a type problem, surely the error would say something about it?
Matt H
I just tested code like that in one of my projects and it works as expected. It must have something to do with your sprite class.
Maynza
It can't be because even this doesn't work: new Vector2().X += 0.1f;What version of XNA are you using?
Matt H
Version 3.0. Do the other methods work?
Maynza
@Maynza: It's because structs have value-type semantics and reading the property returns a temporary copy, not the specific piece of memory which is stored in the class. @Matt: Did you ONLY write that part for your line? I'm pretty sure `new Vector2()` will be considered a temporary copy - and you're not allowed to modify temporary copies of stuff. Try putting the vector into a local variable BEFORE modifying it.
Michael Madsen