0

I am using SFML 2.1 in Code Blocks and I can't figure out how to use Vectors to make clones of my asteroid sprite. It keeps saying that asteroid_V hasn't been declared, and a warning box pops up saying it is "using characters that are illegal in the selected coding" and that they "were changed to protect [me] from losing data".

The objective of this program is to continuously create asteroid sprites that will spawn at random points above the screen before dropping straight down. There were other sprites and aspects in the program but I removed them from this post to properly condense it. This appears to be the only problem after all.

int n;

int main()
{

    RenderWindow window;
    window.setFramerateLimit(30);
    RenderWindow mainMenu;

    srand( time(0));

    Texture asteroid_Pic;
    asteroid_Pic.loadFromFile("Asteroid.png");
    std::vector<sf::Sprite> asteroid(n, Sprite(asteroid_Pic));
    for (int i = 0; i < asteroid.size(); i++){
        asteroid[n].setOrigin(15, 15);
        asteroid[n].getPosition();
        asteroid[n].setPosition(x = rand() % 790 + 10, y = rand() % -10 - 50);
    }

    // run the program as long as the window is open
    while (window.isOpen())
    {
        // check all the window's events that were triggered since the last iteration of the loop
        Event event;
        while (window.pollEvent(event))
        {
            // "close requested" event: we close the window
            if (event.type == Event::Closed){
                window.close();
            }

            asteroid[n].setPosition(x, y+=1);
            asteroid[n].rotate(1);

            // clear the window with black color
            window.clear(Color::Black);

            // draw everything here...
            // window.draw(...);
            window.draw(player1);
            window.draw(asteroid[n]);

            // end the current frame
            window.display();
        }

        return 0;
    }

1 Answers1

0

You have another while (window.isOpen()) inside your main loop. Your program will enter the main loop and then never get out of that inner loop. It will never get to drawing at least once.

You need to get rid of the inner while (window.isOpen()) loop and find another way.

Although the original question was about timers, you can find a basic explanation of a game loop here. You have to handle time in you loop if you want to do something (move sprites, create new ingame entities) based on time.

Community
  • 1
  • 1
nvoigt
  • 75,013
  • 26
  • 93
  • 142