• Advertisement
Sign in to follow this  

Help with Worm Clone

This topic is 1740 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am having trouble with a worm clone I am making. I need help in figuring out how to draw one piece of the body in front of the other. I had it working with a grid system using points. However, it felt "laggy" when it moved from point to point. So instead I am trying to move it along by using Vectors. I currently have the body textures of the worm in a List. I was trying to use a List for the vectors, however I keep receiving an error that it is not a variable. Any suggestions would be much appreciated.

 

for (int i = 0; i < wormBodyTexture.Count; i++)
            {
                wormBodyPosition.X += wormBodyWidth;
            }

 

Share this post


Link to post
Share on other sites
Advertisement

is X a property? If so, does it have a set mutator, and does this work?

wormBodyPosition.X = wormBodyWidth + wormBodyPosition.X;

 

Can you paste the error here, can helpful more that way.

Edited by Andy474

Share this post


Link to post
Share on other sites

is X a property? If so, does it have a set mutator, and does this work?

wormBodyPosition.X = wormBodyWidth + wormBodyPosition.X;

 

Can you paste the error here, can helpful more that way.

I tried your suggestion with the similar results. Here is the error it is giving me:

 

Error    1    Cannot modify the return value of 'System.Collections.Generic.List<Microsoft.Xna.Framework.Vector2>.this[int]' because it is not a variable    C:\Users\Travis\Documents\Wiggles\Wiggles\Wiggles\Wiggles\Wiggles.cs    82
 

Share this post


Link to post
Share on other sites

Vector2 is a struct and therefore a value type. When you return value from list you actually get a copy of the value instead of reference. That is why you can't modify the original value in list.

 

EDIT: This should work not sure if it is a good way to do it though:

 

var temp = wormBodyPosition[i];

temp.X += wormBodyWidth;

wormBodyPosition[i] = temp;

Edited by MrZeo

Share this post


Link to post
Share on other sites

Assign a new Vector2 to it with appropriate values like so:

 

wormBodyPosition = new Vector2((wormBodyPosition.X + wormBodyWidth), wormBodyPosition.Y);

Share this post


Link to post
Share on other sites

@MrZeo, that makes sense. I didn't know that Vector2 was a struct thus using values instead of references.

@Hypotomus, thank you, your solution worked, however I am running into a new error when it goes to the draw method. I am getting: Object reference not set to an instance of an object.

Share this post


Link to post
Share on other sites

fyi, if you were using an array for wormBodyPosition/wormBodyTexture, then you wouldn't have the copy problem (since array indexers are first class .net citizens, and don't return a copy of the object likst List). Of course, that only makes sense to use an array if wormBodyPosition/wormBodyTexture don't change size.

 

Object reference not set to an instance of an object.

 

That generally means you're trying to call a method on a null object. The debugger should point to the line where this happens and tell you which object is null.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement