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

Author Topic: Problem with sequencing events  (Read 2818 times)

0 Members and 1 Guest are viewing this topic.

aBallofWin

  • Jr. Member
  • **
  • Posts: 99
    • View Profile
Problem with sequencing events
« on: January 07, 2012, 04:07:00 pm »
Hey, for part of my game, I will be making a story part, and an rpg part. For the story part, i've designed it so that text will be displayed using a typewriter effect, you wait for that to stop, wait something like 5 seconds then it clears that and goes onto the next string. Well, i've tried while loops, do loops, if statements and even switches, but it's not working.

The closest i've come to it actually working is with while loops, this is an example of the closest i've come to making it work:

Code: [Select]

while (progress == 0)
{
    if (timer.GetElapsedTime() > 0.01 && character < str.length())
            {
                timer.Reset();
                character++;
                text.SetText( str.substr(0, character) ) ;

                App.Clear();
                App.Draw(text);
                App.Display();
            }

            if (character == str.length())
                {
                    sf::Sleep(5);
                    progress = 1;
                }
}


That piece of code will go through the string (str) and display it letter by letter, and this works. To then get it out of the loop, I then put an if statement to know when it's finished and put progress to 1, to make it get out of the loop, and then carry on with another while loop where it will be "while (progress == 1)".

The thing is, it stops all together after progress = 1 and doesn't exit the loop. To make sure that it did actually make progress 1, i did this:

Code: [Select]

if (character == str.length())
                {
                    sf::Sleep(5);
                    std::cout << "Test";
                    progress = 1;
                    std::cout << progress;
                }


and it actually put test, and 1 to the console but just stopped dead :/

If anyone could tell me what I'm doing wrong, or come up with another way round it, i'd be thrilled!

When i've made this, I'll be submitting it to the forums, with the source code, as other people's source code has really helped me learn, and i wanna give back to the community :)

aBallofWin

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
Problem with sequencing events
« Reply #1 on: January 07, 2012, 05:02:08 pm »
Are u really sure that the code doesn't hang after the loop?
I tried the code and it didn't hang.
TGUI: C++ SFML GUI

aBallofWin

  • Jr. Member
  • **
  • Posts: 99
    • View Profile
Problem with sequencing events
« Reply #2 on: January 07, 2012, 05:18:46 pm »
Cheers for the reply, after looking at how it hangs, i come up with a solution.

I thought because character equals str.length() it will stay in the if statement and won't get a change to get out, so i just put character = 0; in the last if statement, and it exited the while loop correctly and displayed the next one!

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
Problem with sequencing events
« Reply #3 on: January 07, 2012, 07:11:35 pm »
This makes no sense. Ifs are not loops.

aBallofWin

  • Jr. Member
  • **
  • Posts: 99
    • View Profile
Problem with sequencing events
« Reply #4 on: January 07, 2012, 07:49:45 pm »
Quote from: "Tex Killer"
This makes no sense. Ifs are not loops.


Yeah I know that, it's pretty weird to be honest, but putting character = 0 in that if statement got it to exit the while loop

 

anything