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 9018011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:24:44+00:00 2026-06-16T04:24:44+00:00

I try to program a top-down-shooter in SFML at the moment, but ran into

  • 0

I try to program a top-down-shooter in SFML at the moment, but ran into a problem. I’m quite new to C++ and programming in general, so please excuse messy code and/or overly complicated solutions.

I have two std::lists, one containing the randomly spawning enemies, the other containing the bullets I fired. When a bullet hits a enemy, both of them should get erased, but it doesn’t work.

Here’s the problem-part of my code:

for(MonsterIt = MonsterList.begin(); MonsterIt != MonsterList.end(); MonsterIt++)
{

    //Here would be Monster-Movement

    //Collision Monster-Player (MonsterIt = iterator of MonsterList)
    if ((MonsterIt -> getPosition().x + 25) >= PlayerX - 25 && 
        (MonsterIt -> getPosition().x - 25) <= PlayerX + 25 &&
        (MonsterIt -> getPosition().y + 25) >= PlayerY - 25 && 
        (MonsterIt -> getPosition().y - 25) <= PlayerY + 25   )
    {
        MonsterList.erase(MonsterIt);
        break;              
    }


        window.draw(*MonsterIt);
}

That’s the way I’ve done collision between Monster and Player. That worked fine, so I tried the same with Monsters and Lasers:

for(LaserIt = LaserList.begin(); LaserIt != LaserList.end(); LaserIt++)
{

    //Here would be "Laser-Movement"

    //Collision-Laser                               // Doesn't work
    if ((MonsterIt -> getPosition().x + 25) >=      //
        (LaserIt   -> getPosition().x - 7) &&       //
        (MonsterIt -> getPosition().x - 25) <=      //
        (LaserIt   -> getPosition().x + 7) &&       //
        (MonsterIt -> getPosition().y + 25) >=      //
        (LaserIt   -> getPosition().y - 7) &&       //
        (MonsterIt -> getPosition().y - 25) <=      //
        (LaserIt   -> getPosition().x + 7))         //
    {                                               //
        MonsterList.erase(MonsterIt);               //
                                                    //
        LaserList.erase(LaserIt);                   //
                                                    //
        break;                                      //
    }                                               //  

    window.draw(*LaserIt);
}

When I put in the part of code I marked (with // on right side) I get a “list iterator not dereferencable”-error while debugging as soon as I shoot. When I cut out said code it runs fine (I can shoot, walk into monsters and they disappear, etc.). Because of that I guess the rest of my code is working.

So, is collision between iterators of different lists even possible?
And if so, how do I do it?

If you need more information or code, please ask. I’d be glad for your help…

  • 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-16T04:24:46+00:00Added an answer on June 16, 2026 at 4:24 am

    The iterator you use is not valid.

    There are several solutions to your problems. First one:

    for(MonsterIt = MonsterList.begin(); MonsterIt != MonsterList.end(); MonsterIt++)
    {
        //Here would be Monster-Movement
    
        //Collision Monster-Player (MonsterIt = iterator of MonsterList)
    
        // Collision with laser, inside the loop for monsters
        for(LaserIt = LaserList.begin(); LaserIt != LaserList.end(); LaserIt++)
        {
        }
    
        window.draw(*MonsterIt);
    }
    

    This solution is not really C++ish. With OOP, you can have a much clearer code:

    class Player
    {
        public:
            int X, Y; // for code simplicity on SO.
    };
    // ...
    
    Player player;
    for(MonsterIt = MonsterList.begin(); MonsterIt != MonsterList.end(); MonsterIt++)
    {
        MonsterIt->update(player,LaserList);
        if(!MonsterIt->isAlive())
        {
            // Remove monster from list *without* break
        }
    }
    

    Then, Monster is a class like the following:
    class Monster
    {
    bool alive;
    public:
    bool isAlive() const { return alive; }

            bool collideWithPlayer(Player p) const
            {
                // Returns whether it collide with player
                // ...
            }
    
            bool collideWithLaser(Laser l) const
            {
                // Returns whether it collide with one laser
                // ...
            }
    
            bool collideWithLasers(LaserList l) const
            {
                for(LaserIt = LaserList.begin(); LaserIt != LaserList.end(); LaserIt++)
                    if(collideWithLaser(*LaserIt))
                        return true;
                return false;
            }
    
    
            void update(Player p, LaserList l)
            {
                if(collideWithPlayer(p) || collideWithLasers(l))
                    alive = false;
                else
                    window.draw(this);
            }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm running into an irritating problem where my program keeps crashing if I try
When I try my program on my win2k8 machine it runs fine but on
In my program I generate classes dynamically but when I try: String[] args =
In the following program I try to pass a structure to a function. But
I am trying to write a blackjack program, but when I try to send
I try to write a program that shows an additional (task)bar on the top
At the top of my program I have #include SDL/SDL_image.h And when I try
I try to transfer into some CMake program and there is a function generating
i try to program a LdapAuthentication and i need some help. First i need
I wanted to try a program distributed in source for *nux on Windows. It's

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.