Sign in to follow this  
Xardov

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

Recommended Posts

Xardov    112
[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
szecs    2990
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
Xardov    112
[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
rip-off    10976
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

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