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

Author Topic: Project Keeps Closing without Error when mouse moved very slow or fast  (Read 1464 times)

0 Members and 1 Guest are viewing this topic.

Mois

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
i am working on a Pong game with multiplayer, currently i am testing some Funktions but unfortunately
it Keeps closing if i move the mouse in a certain way(slowly or fast it doesnt happen always)


while (window.isOpen())
    {
        /* Print out Information about 'player'
        printf("Position-Y: %d\n", rectangle.getPosition().y);
        printf("Size-Y %d\n", (int)rectangle.getSize().y);
        printf("Origin-y: %d\n", rectangle.getOrigin().y);
        Sleep(500);
        */


        while (window.pollEvent(event))
        {
            // Escape-options
            if(event.type == sf::Event::Closed)
            {
                window.close();
            }
            if(event.key.code == sf::Keyboard::Escape)
            {
                window.close();
            }

            // Determining Event-Type
            if(event.type == event.KeyReleased)
            {
                //only to test how to change the lenght of the rectangle
                if(event.key.code == sf::Keyboard::S)
                {
                    rectangle.setSize(sf::Vector2f(rectangle.getSize().x, rectangle.getSize().y *2));
                }
            }
        }

        if(abs(rectangle.getPosition().y - sf::Mouse::getPosition(window).y) > 5)
        {
            if(rectangle.getPosition().y - sf::Mouse::getPosition(window).y > 0)
            {
                rectangle.setPosition(0, rectangle.getPosition().y -(1*acceleration));
                if(acceleration < 3){ acceleration++;}
            }else
            {
                rectangle.setPosition(0, rectangle.getPosition().y +(1*acceleration));
                if(acceleration < 3){ acceleration++;}
            }
        }else
        {
            //this part could be wrong but i suppose it has nothing to do with my problem
            if(acceleration >= 3)
            {
                acceleration = 1;
            }
        }
 

i only inserted the part where i think the problem might be, the rest is only initializing and defining variables

Thank's for any Help :)

Mois

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Update:
It exits if i slowly near the Mouse to the rectangle from the x-Axes

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Run it through the debugger and check values etc.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Mois

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
i did but i dont understand why it keeps closing i only call the Mouse position and my rectangle moves like i wanted it to move all values i set should be fine.

i thought maybe it jumps out of the game loop, but why ??

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
    if(event.key.code == sf::Keyboard::Escape)
    {
        window.close();
    }
 

You don't check this event is a key press first, so, as sf::Event is a union, the key code will have undefined data during other events. In this case the mouse move event is probably triggering window.close()