We are stuck on another issue. The program works fine except how do we make it so if someone is asked what is your name? They can't hit the letter Y or N to skip to the next question? Like I want when a question is asked, for only the keyboard presses to work while that slide is up. Any ideas? Kind hard to explain my question. Here's our code:
#include <SFML/Graphics.hpp>
#include <iostream>
#include <Windows.h>
#include <string>
usingnamespace std;
int main()
{
sf::Event event;
sf::Font font;
if (!font.loadFromFile("arialbd.ttf"))
{
cout << "Could not load the font";
}
sf::RenderWindow Window(sf::VideoMode(1200, 800), "Getting Text On The Screen");
sf::String sentence;
sf::Text text(sentence, font, 40);
text.setColor(sf::Color::Blue);
text.setFont(font);
text.setString("What is your name?");
while(Window.isOpen())
{
sf::Event Event;
while(Window.pollEvent(Event))
{
switch(Event.type)
{
case sf::Event::Closed:
Window.close();
break;
case sf::Event::TextEntered:
if(Event.text.unicode >= 32 &&Event.text.unicode <= 126)
sentence += (char)Event.text.unicode;
elseif(Event.text.unicode == 8 && sentence.getSize() > 0)
sentence.erase(sentence.getSize() - 1, sentence.getSize());
text.setString(sentence);
break;
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Return)){
text.setFont(font);
text.setString("It's nice to meet you Chris!\n Would you like to go on an adventure with me?\n\n Type 'Y' for yes or 'N' for no");
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Y)){
text.setFont(font);
text.setString("That's great news! Let's get started!");
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::N)){
text.setFont(font);
text.setString("Well that's no fun! I'll see you later.");
}
Window.clear(sf::Color::Black);
Window.draw(text);
Window.display();
}
}
This is a very simple logic issue that requires you to track one piece of state. Namely, what are you doing right now? If you're doing thing a, don't do thing b. If you're doing thing b, don't do thing a.
I have a hard time believing you actually spent 3 days trying to figure it out (and have no updated code to show for it.)
#include <SFML/Graphics.hpp>
#include <iostream>
#include <Windows.h>
#include <string>
usingnamespace std;
bool phase1 = true;
bool phase2 = false;
int main()
{
sf::Event event;
sf::Font font;
if (!font.loadFromFile("arialbd.ttf"))
{
cout << "Could not load the font";
}
sf::RenderWindow Window(sf::VideoMode(900, 700), "Getting Text On The Screen");
sf::String sentence;
sf::Text text(sentence, font, 40);
text.setColor(sf::Color::Blue);
text.setFont(font);
text.setString("It's nice to meet you!\nWould you like to go on an adventure with me?\n\n Type 'Y' for yes or 'N' for no");
while(Window.isOpen())
{
sf::Event Event;
while(Window.pollEvent(Event))
{
switch(Event.type)
{
case sf::Event::Closed:
Window.close();
break;
case sf::Event::TextEntered:
if(Event.text.unicode >= 32 &&Event.text.unicode <= 126)
sentence += (char)Event.text.unicode;
elseif(Event.text.unicode == 8 && sentence.getSize() > 0)
sentence.erase(sentence.getSize() - 1, sentence.getSize());
text.setString(sentence);
break;
}
}
if (phase1){
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Y)){
text.setFont(font);
text.setString("That's great news! Let's get started!");
phase1 = false;
phase2 = true;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::N)){
text.setFont(font);
text.setString("Well that's no fun! I'll see you later.");
}
}
if (phase2){
text.setString("When do you want to start the trip?");
}
Window.clear(sf::Color::Black);
Window.draw(text);
Window.display();
}
}