Sign in to follow this  

Removing a Lists's element at a given location in an array (C#/XNA)

This topic is 2010 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

[font=arial,helvetica,sans-serif][size=3][color=#000000][1] [url="http://pastebin.com/77vx6HZx"]http://pastebin.com/77vx6HZx[/url][/color][/size][/font]

[font=arial,helvetica,sans-serif][size=3][color=#000000]Hello! I've got some code here that places trees based on whether the position in the 2D array has a 1 or 0 occupying it. If it is a 1, it places a new Tree at the location, and if it is a 0, it should remove the tree. I have two problems though.[/color][/size][/font][list]
[*][font=arial,helvetica,sans-serif][size=3]
Because the trees are stored in a List<>, using Remove() on them will only remove the first tree in the list, and not the tree at the proper coordinates, trees[x,y]. I need to be able to remove the trees at a specific position in the array.[/size][/font]
[*][font=arial,helvetica,sans-serif][size=3]
I also need to be able to check if a tree is already in the tile before adding a new Tree. However, I have no way to do this. In the code, it merely checks for a 1 in the array and adds a tree based on that.[/size][/font]
[/list]
[font=arial,helvetica,sans-serif][size=3][color=#000000]Got any tips for me?[/color]

[color=#000000]Language is C# with XNA[/color][/size][/font] Edited by Xardov

Share this post


Link to post
Share on other sites
It's not really clear what you are saying. Do you want to place trees at random?

1. It's not clear for me what you are saying. you say you have 0 for no tree and 1 for tree. Then set either 0 or 1. What do I miss?
What/why do you want to remove? You have a static* list that you want to populate with trees or empty fields. Am I right?

*by static I mean you don't have to alter its sizes after you created/loaded the level or whatever.

2. Do you mean you want to place a certain number of trees, and you can't let fewer trees than that? If so: make a while loop, and loop until you find a non occupied cell and place the tree there.


EDIT: maybe I'm slowly getting it. I'd probably use a simpler design for the task than you or just rebuild the whole Tree list every time, so I think you can ignore my post. Edited by szecs

Share this post


Link to post
Share on other sites
[quote name='szecs' timestamp='1339419128' post='4948142']
It's not really clear what you are saying. Do you want to place trees at random?

1. It's not clear for me what you are saying. you say you have 0 for no tree and 1 for tree. Then set either 0 or 1. What do I miss?
What/why do you want to remove? You have a static* list that you want to populate with trees or empty fields. Am I right?

*by static I mean you don't have to alter its sizes after you created/loaded the level or whatever.
[/quote]

I have a 2D array that represents the map. 0 equals no tree, 1 equals tree. I want to be able to remove a tree from the tree list at that specific location if the 1 changes to a 0.

Share this post


Link to post
Share on other sites
Why are you storing the same information twice? Either have a two dimensional map with boolean values indicating the existence of the tree, or have a List<> with Tree instances in it. Doing both is wasteful and complicated.

Share this post


Link to post
Share on other sites

This topic is 2010 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this