Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to make spawn delay to my object  (Read 1955 times)

0 Members and 1 Guest are viewing this topic.

bakar12344

  • Newbie
  • *
  • Posts: 2
    • View Profile
How to make spawn delay to my object
« on: March 12, 2020, 12:26:55 am »
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;
}
 
« Last Edit: March 12, 2020, 07:47:47 am by Laurent »

KokosNaPalmie

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: How to make spawn delay to my object
« Reply #1 on: March 15, 2020, 01:29:39 am »
I am a newbie too, but try to use sf::Clock, with function getElapsedTime(), should work
Example code:
#include <SFML/Graphics.hpp>
#include <iostream>
#include <cstdlib>
int main()
{
    sf::RenderWindow window(sf::VideoMode(1024, 600), "Example code");

    sf::RectangleShape shape(sf::Vector2f(128, 75));     // rectangle for ex
    shape.setOrigin(sf::Vector2f(128 / 2.f, 75 / 2.f));
    shape.setPosition(512, 300);
    shape.setFillColor(sf::Color::Black);

    sf::Clock clock;
    float time_interval;

    while (window.isOpen())
    {
        time_interval = clock.getElapsedTime().asSeconds();

        sf::Event event;

        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed) // closing the window
                window.close();
        }

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) // resetting time
        {
            shape.setFillColor(sf::Color::Black);
            clock.restart();
        }

        if (time_interval > 3) // checking if time is more than 3 seconds
        {
            shape.setFillColor(sf::Color::Blue);
        }

        system("cls");
        std::cout << time_interval << '\n'; // printing time

        window.clear(sf::Color::White);

        window.draw(shape);

        window.display();
    }
}

if you want to see more about that, see some youtube tutorials (like Suraj Sharma's SFML RPG)