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

Author Topic: How do I move an object once every a constant time interval  (Read 1608 times)

0 Members and 1 Guest are viewing this topic.

chrisamgad

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
How do I move an object once every a constant time interval
« on: April 02, 2018, 06:34:39 pm »
for example I have
sf::RectangleShape player(sf::Vector2f(100.f,100.f))

How do I make the player move once like every 500 milliseconds upwards for example?

Fililip

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: How do I move an object once every a constant time interval
« Reply #1 on: April 02, 2018, 07:18:53 pm »
You can create an sf::Clock and check the elapsed time every frame. And after it reaches the timer threshold you want, you move your player quad and reset the clock.

Example code:
sf::RectangleShape player(sf::Vector2f(100.f,100.f));
sf::Clock timer;

//in the game loop
if (timer.getElapsedTime().asMilliseconds() >= 500)
{
    player.move(0, 5); // replace 5 with anything you want
    timer.restart();
}
 

chrisamgad

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: How do I move an object once every a constant time interval
« Reply #2 on: April 02, 2018, 07:44:15 pm »
You can create an sf::Clock and check the elapsed time every frame. And after it reaches the timer threshold you want, you move your player quad and reset the clock.

Example code:
sf::RectangleShape player(sf::Vector2f(100.f,100.f));
sf::Clock timer;

//in the game loop
if (timer.getElapsedTime().asMilliseconds() >= 500)
{
    player.move(0, 5); // replace 5 with anything you want
    timer.restart();
}
 

Cheers mate ;)