neamus neamus - 25 days ago 8
C# Question

Making An Object Move C#

I'm making a game in c# and I'm trying to make my texture move. I need some help and advice to make the texture move. This is what I've used so far.

public float InitialTime { get; set; }

public Vector2 InitialPosition { get; set; }

public Vector2 InitialVelocity { get; set; }

public Vector2 Position(GameTime time)
float t = (float)time.TotalGameTime.TotalSeconds = InitialTime;
return InitialPosition + t * InitialVelocity;

on the 'float t' line, it comes up with the following error,"
Error CS0131 The left-hand side of an assignment must be a variable, property or indexer"


Your problem is because of this:

float t = (float)time.TotalGameTime.TotalSeconds = InitialTime;

Both sides of an assignment operation need to have valid syntax. If you were to isolate the second assignment, it would look like this:

(float)time.TotalGameTime.TotalSeconds = InitialTime;

Which is not only invalid syntax, but it also makes little sense.

I imagine that the reason you are doing the cast is because TotalSeconds and InitialTime are doubles and you need to cast them to a float. There are two ways to accomplish this.

You can either split up the assignment onto two different lines:

time.TotalGameTime.TotalSeconds = InitialTime;
float t = (float)time.TotalGameTime.TotalSeconds;

Or, if you insist on doing this on a single line, use parentheses to group your operations:

float t = (float)(time.TotalGameTime.TotalSeconds = InitialTime);


Then again, I've been assuming that the double assignment was intentional. What you may have meant to do was to subtract InitialTime from your TotalTime to get the current time stamp. In that case, it's just a simple fix of a typo:

float t = (float)(time.TotalGameTime.TotalSeconds - InitialTime);

(Note that if TotalSeconds and InitialTime are doubles, you will need to use parentheses to cast the operation to a float, as I have done.)