The tutorial I’m following (http://www.bluerosegames.com/xna101/post/Lesson-9-Improving-the-BouncingBall-Class-Using-Properties.aspx)
private Vector2 _position;
public Vector2 Position
{
get
{
return _position;
}
set
{
_position = value;
}
}
does not use the automatic properties feature in the code. I’d like to skip most of the typing using the automatic properties but I have problems later on in the code. I’ve commented the lines below that are affected.
public Vector2 Position { get; set; }
public void Update()
{
Position = Position + Velocity;
if (Position.X < 0 || Position.X > GraphicsViewport.Width - Texture.Width)
{
// If we get in here, we've hit a vertical wall
Velocity.X = -Velocity.X; // doesn't work
Position.X = Position.X + Velocity.X; // doesn't work
}
if (Position.Y < 0 || Position.Y > GraphicsViewport.Height - Texture.Height)
{
// If we get in here, we've hit a horizontal wall
Velocity.Y = -Velocity.Y; // doesn't work
Position.Y = Position.Y + Velocity.Y; // doesn't work
}
}
The error that I get in Visual Studio states that “Cannot modify the return value of WindowsGame1.BouncingBall.Position’ because it is not a variable.” So how can I get the code to work with the automatic properties?
Thank you!
John
Your exact question has already been asked and answered: