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

Author Topic: Keyboard event not working.  (Read 1739 times)

0 Members and 1 Guest are viewing this topic.

Xrey274

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Keyboard event not working.
« on: June 01, 2018, 05:26:23 pm »
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <iostream>
#include <string>

using namespace std;

class Dot
{
    private:
        sf::CircleShape dot;
        sf::Mouse mouse;
        sf::Vector2f position;

    public:
        sf::CircleShape drawDot()
        {
            position = static_cast<sf::Vector2f>(mouse.getPosition());

            dot.setRadius(150);
            dot.setFillColor(sf::Color::Green);
            dot.setPosition(position);

            return dot;
        }
};


int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 800), "Snake");
    sf::Event event;
    sf:: Time time = sf::seconds(5);

    Dot dots;

    while(window.isOpen() == true)
    {
        while(window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed)
            {
                window.close();
            }

            if(event.type == sf::Event::KeyPressed)
            {
                sf::CircleShape gm;
                gm = dots.drawDot();
                window.draw(gm);
            }
            window.clear(sf::Color::Black);

            window.display();
        }
    }
}

When debugging it completly skips the keyboard if statement.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Keyboard event not working.
« Reply #1 on: June 01, 2018, 06:02:37 pm »
It isn't, but right after drawing, you clear the whole screen and then display an empty screen. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Xrey274

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: Keyboard event not working.
« Reply #2 on: June 02, 2018, 04:02:58 pm »
I tweaked the code a bit and now I am getting a "No match for operator[]" at line 53;

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>

using namespace std;

sf::CircleShape drawDot(sf::Vector2f position)
{
    sf::CircleShape dot;

    dot.setRadius(rand());
    dot.setFillColor(sf::Color::Green);
    dot.setPosition(position);

    return dot;
}

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 800), "Little Painter");
    sf::Event event;
    sf::Vector2f position;
    sf::Mouse mouse;

    vector<sf::CircleShape> circles;

    while(window.isOpen() == true)
    {
        while(window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed)
            {
                window.close();
            }

            if(event.type == sf::Event::KeyPressed)
            {
                position = static_cast<sf::Vector2f>(mouse.getPosition(window));

                circles.push_back(drawDot(position));


            }

            window.clear(sf::Color::Black);

            for(vector<sf::CircleShape>::iterator it = circles.begin(); it != circles.end(); it++)
            {
                window.draw(circles[it]);
            }

            window.display();


        }

    }
}
« Last Edit: June 02, 2018, 04:49:00 pm by eXpl0it3r »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Keyboard event not working.
« Reply #3 on: June 02, 2018, 06:32:07 pm »
That's not how C++ vector iterators work.

Xrey274

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: Keyboard event not working.
« Reply #4 on: June 02, 2018, 07:21:28 pm »
How do they work?

Xrey274

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: Keyboard event not working.
« Reply #5 on: June 02, 2018, 07:35:09 pm »
Lol i remember now, sorry for the trouble.

 

anything