🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

HOW TO MAKE SPAWN DELAY ON SFML

Started by
2 comments, last by bakar12344 4 years, 3 months ago
/*Hello, I am a newbie.
I have problem with my program,i make a program to spawn some object falling from top with random area, but my object spawn together,can you teach me how to make my object spawn with some delay. Thanks

My Code*/

#include <SFML/Graphics.hpp>

#include <stdlib.h>

#include <time.h>



using namespace sf;



int main()

{

    srand(time(0));

    RenderWindow window(VideoMode(1200,600),"PROJECT");



    int k = 0;

    float x[50],xb[50],y[50];



    for(int i = 0; i < 50; i++){

        x[i] = 0;

        y[i] = 0;

    }



    Texture buah1,latar;

    buah1.loadFromFile("pir.png");

    latar.loadFromFile("background.png");



    Sprite pir(buah1),background(latar);



    while(window.isOpen()){

        Event tutup;

        while(window.pollEvent(tutup)){

            if(tutup.type == Event::Closed)

                window.close();

        }

        window.clear(Color::White);



        window.draw(background);



        for(int i = 0; i < 50; i++){

            if(k == i){

                xb[i] = rand() % 1200;

                x[i] = xb[i];

                k++;

                break;

            }

            else continue;

            }



        for(int i = 0; i < 50; i++){

            pir.setScale(0.2, 0.2);

            pir.setPosition(x[i],y[i]);

            y[i] += 0.5;

            window.draw(pir);

        }

        window.display();

    }

    return EXIT_SUCCESS;

}
Advertisement

Spawn delay would require some form of a timer.

https://www.sfml-dev.org/tutorials/2.5/system-time.php

I use a fixed time step for my updates so I just set the rate based on my update frequency and factor in how often I want to spawn an item. Example, if I update 30 times per second, but I want my spawn to occur every 5 seconds I would have a counter that would increase by one per update cycle and when it hits 150 updates (with 30 updates per second you would have 5 seconds pass to reach 150 - assuming perfect ticks) then I spawn the item and reset the counter.

Otherwise you can use the time passed in your update to verify if you should spawn in or not. Example would be if you spawn at level start or whenever a trigger is set you need a variable which keeps some kind of a target for your next spawn (current time + 5 seconds). So when your update cycles through you'll check the variable for the elapsed time and when it equals your target time for the spawn you spawn the entity and reset the target to current time + 5 seconds again, ect…

Another option is to set up another timer. I personally don't do this because I don't like any timer operating other than a single call prior to my update cycle. It isn't a good practice to call your clock over and over, it makes more sense to call it once prior to each update cycle, store the value in a variable and use that as a reference throughout until your next cycle…

Programmer and 3D Artist

@undefined okay thxx bro

This topic is closed to new replies.

Advertisement