Help with Worm Clone

Started by
5 comments, last by phil_t 11 years ago

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;
            }

Cpl Alt, Travis A

USMC

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.

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

Cpl Alt, Travis A

USMC

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;

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


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

@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.

Cpl Alt, Travis A

USMC

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.

This topic is closed to new replies.

Advertisement