1
General / Re: How to make spawn delay to my object
« 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:
if you want to see more about that, see some youtube tutorials (like Suraj Sharma's SFML RPG)
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();
}
}
#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)