Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Help with Worm Clone


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Manhattanisgr8   Members   -  Reputation: 487

Like
0Likes
Like

Posted 19 April 2013 - 02:49 AM

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[i].X += wormBodyWidth;
            }

 


Cpl Alt, Travis A

USMC


Sponsor:

#2 Andy474   Members   -  Reputation: 690

Like
1Likes
Like

Posted 19 April 2013 - 04:32 AM

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

wormBodyPosition[i].X = wormBodyWidth + wormBodyPosition[i].X;

 

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


Edited by Andy474, 19 April 2013 - 04:33 AM.


#3 Manhattanisgr8   Members   -  Reputation: 487

Like
0Likes
Like

Posted 19 April 2013 - 09:32 AM

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

wormBodyPosition[i].X = wormBodyWidth + wormBodyPosition[i].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


#4 MrZeo   Members   -  Reputation: 116

Like
1Likes
Like

Posted 19 April 2013 - 02:23 PM

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, 19 April 2013 - 02:25 PM.


#5 Hypostomus   Members   -  Reputation: 224

Like
1Likes
Like

Posted 19 April 2013 - 03:30 PM

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

 

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


#6 Manhattanisgr8   Members   -  Reputation: 487

Like
0Likes
Like

Posted 20 April 2013 - 03:14 PM

@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


#7 phil_t   Crossbones+   -  Reputation: 4109

Like
1Likes
Like

Posted 20 April 2013 - 03:23 PM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS