Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8257179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:06:46+00:00 2026-06-08T02:06:46+00:00

I have been looking for the reason why I get this error message for

  • 0

I have been looking for the reason why I get this error message for several days now! And I need help to solve this or to improve the code. It’s hard to understand why this error happens and find the reason, when it just happens sometimes and not all the time! But my guess is that it has to do with the list and the numbers of items in the lists. It’s in the second part of the code where the error event happens. I have also tried to add the objects that I want to remove in a special “to remove list”, but why should this not work? Help is appreciated! Thanks!

public void CollisionControlMissileHitAsteroid(ContentManager content)
{
    for (int i = 0; i < missilesList.Count(); i++)
    {
        // Stora asteroider
        for (int j = 0; j < asteroidsBigList.Count(); j++)
        {
            if (missilesList.ElementAt(i).Bounds().Intersects(asteroidsBigList.ElementAt(j).Bounds())) // Fel här ??
            {

                for(int x = 0; x < 2; x++)
                AddNewSmallAsteroidToList(new AsteroidSmall(content, asteroidsBigList.ElementAt(j).Position));
                missilesList.RemoveAt(i);
                i--;

                asteroidsBigList.RemoveAt(j);
                j--;
            }
        }


        if (missilesList.Count() > 0 && asteroidsSmallList.Count > 0)
        {

            for (int k = 0; k < asteroidsSmallList.Count(); k++) 
            {
                if (missilesList.ElementAt(i).Bounds().Intersects(asteroidsSmallList.ElementAt(k).Bounds())) // THIS IS WHERE THE ERROR EVENT HAPPENS!
                {
                    missilesList.RemoveAt(i);
                    i--;
                    asteroidsSmallList.RemoveAt(k); 
                    k--;
                }
            }
        }
    } 
}

EDIT:

Is this where I should place the break? Ask beacause it still happens! I can play for five minutes until it happens!

if (missilesList.Count() > 0 && asteroidsSmallList.Count() > 0)
{

    for (int k = 0; k < asteroidsSmallList.Count(); k++) 
    {
        if (missilesList.ElementAt(i).Bounds().Intersects(asteroidsSmallList.ElementAt(k).Bounds())) 
        {
            missilesList.RemoveAt(i); 
            i--;
            asteroidsSmallList.RemoveAt(k);
            k--;
            break; // ???????
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T02:06:48+00:00Added an answer on June 8, 2026 at 2:06 am

    Suppose you start off with one missile and two asteroids. The missile (i=0) hits the first asteroid (j=0) – but you’re then continuing with i=-1 and j=0. You should be breaking out of the inner loop and continue with the next iteration of the outer loop there instead. After all, you’re “done” with the missile – it can’t hit any other asteroids, big or small.

    (And yes, as per xanatos’s comments, it would be more idiomatic to use the Count property instead of the Count() method.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been looking for an answer to this on Stack Overflow, but I
I have been looking at this for to long so I am tossing it
I have been working on this for the past 5 hours. For some reason
I've been looking for a reason why this doesn't work, but I can't find
I have been looking for an answer for this, but none seem to actually
After a lot of help from people on this website I have been able
i need a pointer i the right direction. I have been looking around and
I have been looking for an alternative to Hibernate for various reasons. I came
Have been looking on some tutorials for drawing canvas using SurfaceView, but the only
I have been looking around for a visualization framework that would aid graph visualization

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.