C++ and SDL Trouble Creating a STL Vector of a Game Object

Posted by Jackson Blades on Game Development See other posts from Game Development or by Jackson Blades
Published on 2012-07-05T16:50:48Z Indexed on 2012/07/05 21:25 UTC
Read the original article Hit count: 248

Filed under:
|
|

I am trying to create a Space Invaders clone using C++ and SDL. The problem I am having is in trying to create Waves of Enemies. I am trying to model this by making my Waves a vector of 8 Enemy objects.

My Enemy constructor takes two arguments, an x and y offset. My Wave constructor also takes two arguments, an x and y offset. What I am trying to do is have my Wave constructor initialize a vector of Enemies, and have each enemy given a different x offset so that they are spaced out appropriately.

Enemy::Enemy(int x, int y)
{
    box.x = x;
    box.y = y;
    box.w = ENEMY_WIDTH;
    box.h = ENEMY_HEIGHT;

    xVel = ENEMY_WIDTH / 2;
}

Wave::Wave(int x, int y)
{
    box.x = x;
    box.y = y;
    box.w = WAVE_WIDTH;
    box.y = WAVE_HEIGHT;

    xVel = (-1)*ENEMY_WIDTH;
    yVel = 0;

    std::vector<Enemy> enemyWave;
    for (int i = 0; i < enemyWave.size(); i++)
    {
        Enemy temp(box.x + ((ENEMY_WIDTH + 16) * i), box.y);
        enemyWave.push_back(temp);
    }
}

I guess what I am asking is if there is a cleaner, more elegant way to do this sort of initialization with vectors, or if this is right at all. Any help is greatly appreciated.

© Game Development or respective owner

Related posts about c++

Related posts about vector