Jump to content
  • Advertisement
Sign in to follow this  
myriac

about an error message ...

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

can you help about an error message ? i made a code. i have a list holding some sprites in it. they are moving on the screen. if two's vectoral position of them are the same(conflict) i want to remove them from the list and also screen. foreach (maze ball in listarrows) { foreach (maze ball2 in listarrows) { if (ball !=ball2) { if (ball.Position == ball2.Position) { Console.WriteLine("hello"); listarrows.Remove(balls); } } } above is the comparison of these two sprites. the first line in the loop is ok ! it writes hello when a conflict occurs among the sprites. but the second line says : "Collection was modified; enumeration operation may not execute." can you help me ? how will i remove sprites at run time from the list ? kind regards myriac.

Share this post


Link to post
Share on other sites
Advertisement
I tried a few Googles on the error and this is what I could come up with:

If you remove an item from the same list you're looping through, you run the risk of hitting one that's been deleted.

Safer to use a For loop.

Share this post


Link to post
Share on other sites
Original post by myriac


foreach (maze ball in listarrows)
{
foreach (maze ball2 in listarrows)
{
if (ball !=ball2)
{
if (ball.Position == ball2.Position)
{
Console.WriteLine("hello");
listarrows.Remove(balls);

}
}
}

i cant create the equivalent for loop of above code part.
i am upset indeed .

pls somebody help me !

Share this post


Link to post
Share on other sites
You're trying to remove something in your outer loop which could cause problems. There are also cleaner and more efficient ways than going through two for loops though to get your desired results.

Ex:

Sort the list based on position.
If you have a conflict, don't add the second ball to your sorted list.

Some very simplistic pseudocode


listarrows mycopy = listarrows;
foreach(maze ball in listarrows2)
{
myhashiterator = myhash.find(ball.position);
if(myashiterator != myhash.end() )
{
listarrows.Remove(ball);
}
else
{
myhash.insert(ball.position);
}
}




Note - this by no means is efficient.

[Edited by - Nytegard on May 29, 2008 2:33:12 PM]

Share this post


Link to post
Share on other sites
You shoudn't remove elements from the container when you're visiting the elements in the container iteratively. So, to solve your problem, prepare a list before the loop and put the elements you want to remove from the container into the list, then remove them after the loop.

for example:


// create a list
ArrayList toBeRemovedList = new ArrayList();

foreach(object obj in AllObjectList)
{
if(/* the current object should be removed */)
{
// put it into the "To Be Removed" list
toBeRemovedList.Add(obj);
}
}

// remove them from the container
foreach(object obj in toBeRemovedList)
{
AllObjectList.Remove(obj);
}



By the way, your program is in c# or java, right? Both "Add" and "Remove" methods need "pass by referece" to work well. So if your program is in c++, you should replace the parameter with a pointer or an iterator.

[Edited by - c43892 on May 30, 2008 1:21:03 AM]

Share this post


Link to post
Share on other sites
protected override void Update(GameTime gameTime)
{
KeyboardState keystate = Keyboard.GetState();
if (keystate.IsKeyDown(Keys.Escape))
this.Exit();
float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
fTotalElapsedTime += elapsed;

foreach (maze ball in listarrows)
{
foreach (maze ball2 in listarrows)
{
if (ball != ball2)
{
if (ball.Position == ball2.Position)
{
this.Exit();
Console.WriteLine("hello");
listarrows.Remove(balls);
}
}
}

if (keystate.IsKeyDown(Keys.Space))
{

ball.Velocity = new Vector2(ball.wayx, ball.wayy);
fTotalElapsedTime = 0.0f;
}
ball.Update();
}

if (fTotalElapsedTime >= fKeyPressCheckDelay)
{
if (keystate.IsKeyDown(Keys.Down))
{

listarrows.RemoveAt(listarrows.Count-1);
fTotalElapsedTime = 0.0f;
}
if (keystate.IsKeyDown(Keys.Up))
{
//incarrow++;
basla();
listarrows.Add(arrow);
//index++; ballCount++;
fTotalElapsedTime = 0.0f;
//Console.WriteLine(listarrows.Count);

}
}
base.Update(gameTime);
}

above is the update part of my game1.cs.
it does 3 things :
1)when you press and hold "up arrow" sprites with random positions,color,velocity etc.., added in list from zero increasing by 1 and shown on screen.
2)when you press "down arrow" sprites subtracted from the list and remove from screen.
(these two work very fine as you helped about before)

3)when pressed space key; all sprites created so far(for example 25 sprites), moving randomly.
when they conflict, code writes "hello". but at the same time i want conflicting sprites to be removed from the list and screen also.
so , listarrows.remove(balls); causes an error.


can you implement above foreach loops for me as for loops ?
i did try them but i am stuck about comparable object variables in for loops.

thx in advance

myriac





Share this post


Link to post
Share on other sites
Original post by myriac


foreach (maze ball in listarrows)
{
foreach (maze ball2 in listarrows)
{
if (ball != ball2)
{
if (ball.Position == ball2.Position)
{
this.Exit();
Console.WriteLine("hello");
listarrows.Remove(balls);
}
}
}
}



[/quote]

Again, you are trying to remove an item from a list that you are currently iterating through. As described in the above posts, you cannot do this for various reasons.

You should replace the above code with the code example that c43892 supplied. This is not great performance wise but will do exactly what you want it to. You could also do something like this:


// Create a temp list to loop through
ArrayList tempListarrows = listarrows;

foreach (maze ball in tempListarrows )
{
foreach (maze ball2 in tempListarrows )
{
if (ball != ball2)
{
if (ball.Position == ball2.Position)
{
this.Exit();
Console.WriteLine("hello");
listarrows.Remove(balls);
}
}
}
}




Hope it helps,

James

EDIT: I've just looked at your code and just out of interest why do you have this.Exit in the collision code? Also, I think you'll find that the chances of ball.position and ball2.position being equal are quite low. You may want to look into the BoundingBox objects in XNA - I think you would find these very usfull. Lastly, inside the collision code you have .Remove(balls) - what's "balls"? If you want to remove both balls then do 2 remove operations (.Remove(ball1); .Remove(ball2);).

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!