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

Author Topic: I want to make sprites appear with a random time delay  (Read 12841 times)

0 Members and 1 Guest are viewing this topic.

ikarah

  • Newbie
  • *
  • Posts: 15
    • View Profile
I want to make sprites appear with a random time delay
« on: March 12, 2016, 12:02:01 am »
I want to make certain sprites appear with a random time delay. For some reason just putting in this code in the beginning of main to begin with.


   sf::Clock clock;
      Time time1 = clock.getElapsedTime();
      Time time2 = clock.restart();

It gives me an error that time is an undeclared identifier? How can I go about doing this.  :(

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: I want to make sprites appear with a random time delay
« Reply #1 on: March 12, 2016, 12:07:17 am »
Probably because it's missing the sf:: namespace?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

ikarah

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: I want to make sprites appear with a random time delay
« Reply #2 on: March 12, 2016, 12:08:00 am »
Yes I figured it out by doing this

sf::Clock Clock;
   sf::Time time;
      time = Clock.getElapsedTime();
      Clock.restart();

But how can I make sprites appear at random times within a certain time range somehow?

Also when I do something like this

if (time.asSeconds() >= 10.0f) {
            Window.draw(Player1);
         }

It just doesn't work. The code runs fine but nothing is drawn after the 10 seconds pass
« Last Edit: March 12, 2016, 12:25:16 am by ikarah »

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: I want to make sprites appear with a random time delay
« Reply #3 on: March 12, 2016, 12:50:14 am »
You don't really give enough information about your code to tell what is going wrong. I'll try to take some guesses though.

Quote
sf::Clock Clock;
sf::Time time;
time = Clock.getElapsedTime();
Clock.restart();
 

Clock.getElapsedTime() returns the amount of time since the clock was constructed or last restarted. You are constructing the clock right before this call, so not much time will have passed at all. If you aren't reading from the clock again later in your code in a loop or something then your time variable certainly won't be 10 seconds.

Also, make sure you are displaying your window after you draw to it.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
AW: I want to make sprites appear with a random time delay
« Reply #4 on: March 12, 2016, 12:50:21 am »
Time only holds a certain duration, e.g. like 2s or 500ms, it doesn't change unless you assign it a new value.

You can use the clock and generate random numbers with the functions found in the C++ header <random>.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

ikarah

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: I want to make sprites appear with a random time delay
« Reply #5 on: March 12, 2016, 03:55:00 am »
Ok I will clarify,

As a test I first created the following:

sf::Clock clock;
   sf::Time time;
      time = clock.getElapsedTime();
   while (Window.isOpen()) {
      sf::Event Event;
      while (Window.pollEvent(Event)) {
         sf::Time t1 = sf::seconds(10.0);
         time = clock.getElapsedTime();
         std::cout << time.asSeconds() << std::endl;
         std::cout << t1.asSeconds() << std::endl;

         if (time.asSeconds() >= t1.asSeconds()) {
            std::cout << "player 1 is drawn";

         }

With this code I do in fact get "Player 1 is drawn" on the console after 10 seconds. However when I place the following code,

Window.draw(PlayerTwo); in the if statement and i already defined the sprite image and texture before in main. Nothing gets drawn on my window. What is going on here? There are no errors too.

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: I want to make sprites appear with a random time delay
« Reply #6 on: March 12, 2016, 04:07:34 am »
make sure you are displaying your window after you draw to it.
You did not answer this.
Are you using window.display()?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

ikarah

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: I want to make sprites appear with a random time delay
« Reply #7 on: March 12, 2016, 04:11:23 am »
Yes of course window display and window clear

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: I want to make sprites appear with a random time delay
« Reply #8 on: March 12, 2016, 06:25:20 am »
Where is your clear? Is just before your display (but after your draw)?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

ikarah

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: I want to make sprites appear with a random time delay
« Reply #9 on: March 12, 2016, 02:11:09 pm »
It is after my window draw and window display. Is this what's causing the issue?

ikarah

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: I want to make sprites appear with a random time delay
« Reply #10 on: March 12, 2016, 07:52:03 pm »
Ok so I removed the window clear and my code worked but now the sprite is flashing non stop I want it to be a stable image. What is causing this flashing if the condition of the time has been met?

I figured that this was due to me placing the code in the event poll area instead of the window open area.. Now how can I generate a random time within a certain time range for example. Generate random seconds between 5-10 seconds?

Also I realized that it will continue to draw the sprite continuously since the condition was met I only want it to draw the sprite once and thats it. How can I do that?
« Last Edit: March 12, 2016, 08:01:33 pm by ikarah »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
AW: I want to make sprites appear with a random time delay
« Reply #11 on: March 12, 2016, 09:04:06 pm »
So you removed all clear statements?

You should always clear(), draw(), display().
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

ikarah

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: I want to make sprites appear with a random time delay
« Reply #12 on: March 12, 2016, 09:17:55 pm »
That advice worked perfectly. Now, how can I make random variables?
float x for example

I want x to carry random numbers in a certain range? For example from 5-9

korczurekk

  • Full Member
  • ***
  • Posts: 150
    • View Profile
    • Email
Re: AW: I want to make sprites appear with a random time delay
« Reply #13 on: March 12, 2016, 10:09:01 pm »
So you removed all clear statements?

You should always clear(), draw(), display().
clear() isn't always needed, well, it's useless in almost every bigger project which overwrites whole screen with some background.

That advice worked perfectly. Now, how can I make random variables?
float x for example

I want x to carry random numbers in a certain range? For example from 5-9
Use something like this:

rand() % (9 - 5) + 5;
or
rand() % (9 - 5 + 1) + 5;

EIDT: Or if you want floating point value like 0.01:
static_cast<float>(rand() % (900 - 500) + 500) / 100.f;
« Last Edit: March 12, 2016, 10:11:14 pm by korczurekk »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
AW: Re: AW: I want to make sprites appear with a random time delay
« Reply #14 on: March 12, 2016, 10:18:20 pm »
clear() isn't always needed, well, it's useless in almost every bigger project which overwrites whole screen with some background
This is wrong. SFML requires you to call clear, due to double buffering.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/