2

Im quite stuck here. I have been trying and googling for the past 2 days but I cant figure it out. I have a class that is called Player and another that is called Enemy that inherits from Player. I have a list that stores coordinates for my bullets and loop trough them in Player. Im trying to access and loop trough the same list to check for collision in Enemy that builds on Player, but It will not even enter the loop. I guess somehow its empty but why?

struct structShoot
{
    float x;
    float y;
};

class Player
{
private:
      blablabla
protected:
    list<structShoot>::iterator it;
    list<structShoot> shoot_list;
    structShoot test;
public:
     void render(SDL_Surface* dest);
};

void Player::render(SDL_Surface* dest)
{    
//Works fine, see the other loop down below
for(it = shoot_list.begin(); it != shoot_list.end();)
{
    shoot.moveSet(it->x, it->y);
    shoot.draw(dest);
    it->y--;

    if((it->y) < -25)
    {   
        it = shoot_list.erase(it);
    }

    else
    {
        it++;
    }   

}
}

class Enemy : protected Player
{
 public:
 void render(SDL_Surface* dest);
};

void Enemy::render(SDL_Surface* dest)
{
    SDL_Rect a, b;


    //Does not enter loop!? Ever. Why?
    for(it = shoot_list.begin(); it != shoot_list.end();)
    {
        SDL_Quit();
        a.x = enemy.getX();
        a.y = enemy.getY();
        a.w = enemy.getWidth();
        a.h = enemy.getHeight();

        b.x = it->x;
        b.y = it->y;
        b.w = 10;
        b.h = 19;
        it->y--;


        if (collision(a, b) == true)
        {
            SDL_Quit();
        }

        if(it->y < -25)
        {   
            it = shoot_list.erase(it);
        }

        else
        {
            it++;
        }   


    }
}
Jesper
  • 27
  • 1
  • 3
  • Maybe you erased the whole list in the first loop, so the second loop has nothing to do? – Sasha Goldshtein Apr 11 '11 at 14:21
  • Have you tried running this in a debugger, or adding print statements? For instance, have you checked that `!shoot_list.empty()`? – Oliver Charlesworth Apr 11 '11 at 14:22
  • You're not doing some type polymorphic cast of an `Enemy` object to a `Player` pointer, are you? If that's the case, then because `render()` is not a virtual function, any calls to some pointer `(*my_player).render()` would only call `Player::render()` and not `Enemy::render()`. – Jason Apr 11 '11 at 14:35
  • Are you adding all your bullets to the enemies as well? The `shoot_list` isn't shared between objects. I would suggest that you move the bullet list out of the player object and maintain it separately. And move all your movement and collision detection out of the render functions - it will save you a lot of headache later on. – molbdnilo Apr 11 '11 at 14:36
  • Ok thanks, but is there a way to share shoot_list somehow? – Jesper Apr 11 '11 at 16:34

2 Answers2

2

You should make render virtual to use polymorphism.

virtual void render(SDL_Surface* dest);

I suppose that every time you call Player::render, because of such code:

Player* enemy = new Enemy();
enemy->render(); // there is Player::render calling

If you make render virtual you will use virtual table to detect correct function that should be called in this place. So you have to make render virtual.

  • Im sorry, Im kinda new to OOP. I made the render virtual but Im still having the same problem. Im not sure I understand why you I should make it Player* enemy = new Enemy(); If someone have the time I have uploaded the whole project. Im not asking you to solve it just give me some tips. http://www.megaupload.com/?d=Z1Y9XCO2 – Jesper Apr 11 '11 at 15:03
  • I can't find the place where you fill shoot_list for enemy. Only place that I find is void Game::OnEvent(SDL_Event* Event) case SDLK_SPACE: pl.fire(); break; where you are adding elements to shoot_list of player. I believe there is the root cause of your issue. – Danil Krivopustov Apr 11 '11 at 15:16
  • Im not adding elements to shoot_list for enemy. I would just like to loop trough the elements (coordinates) in Enemy from Player (shoot_list) to check if players bullet have hit the enemy. Does this make sense? :S – Jesper Apr 11 '11 at 16:47
  • Looks like you need to place shoot_list as a member of Game class and populate it there.Then you can access to this list by player and enemy instances. – Danil Krivopustov Apr 11 '11 at 18:47
0

There is only one possible thing that could cause this, namely that shoot_list.begin() is equal to shoot_list.end(), thus it's empty.

Perhaps the loop at Player::render is broken and empties the list completely?

orlp
  • 112,504
  • 36
  • 218
  • 315