lundi 9 octobre 2017

c++ calling function from list of pointers

This might have been asked and/or answered before, but i was unable to find a question here that i could understand. I've been following some SDL tutorials around, and hit upon a problem with a list of pointers.

I have a list of pointers declared like so : std::list < GameObj* > gameObjList; The list is supposed to contain pointers to all the objects currently involved in my game. However, in my main loop, i have this:

///Game logic
    for (GameObj* o : gameObjList) {
        (*o).step();
    }

By adding std::cout functions in strategic places, i realised that the loop is executed once per step, which is okay because i only have one such GameObj created and added to the list. However the std::cout's inside the step() function are apparently not reached.

Here's the step() function of PlayerObj:

void step() {
    std::cout << " a ";
    int _x = 0, _y = 0;
    _x += keys[SDL_SCANCODE_D];
    _x -= keys[SDL_SCANCODE_A];
    _y += keys[SDL_SCANCODE_S];
    _y -= keys[SDL_SCANCODE_W];
    if (_x == 0 && _y == 0)
        setSpeed(0);
    else setSpeed(1);
    setDir(atan2(_y, _x));
    GameObj::step();
}

The last statement, GameObj::step() calls the step function of PlayerObj's parent class, GameObj. If there's anything you need to know, please ask, and i'm sorry if this is a duplicate but it might be too hard for me to understand this early.

Aucun commentaire:

Enregistrer un commentaire