Bonbin
Bonbin

Reputation: 305

Handle mouse events SFML

I have some trouble with events in SFML. I am making a turnbased game, when the mouse has moved or left mouse button is clicked i check whos turn it is and then spawn a projectile at that objects position, when the projectile has collided with either terrain or opponent it gets destroyed and the turn is changed.

The behaviour is not what i am expecting though. When clicking shoot the projectile sometimes wont spawn at all (and it changes the turn immedeately). I have disabled all collisions so it cant be that. Im 90% sure that the issue is with how i handle events, so id really appretiate input on how i can make it better.

Of what ive learned is that you should not execute the functions in the while poll event, its only for registering what has happend most recently,so i put them outside instead. This does not solve my problem though...

    sf::Event event;
    sf::Vector2i mousePos;
    while (_window.pollEvent(event)) {
        if (event.type == sf::Event::Closed) {
            _window.close();
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
            _window.close();
        }
        if(event.type == sf::Event::MouseMoved) { // <- this is how you check to see if an event
            mousePos = sf::Mouse::getPosition();
            moved = true;
        }
        if(event.type == sf::Event::MouseButtonPressed) { // <- this is how you check to see if an event
            mousePressed = true;
        }
    }

    if (mousePressed && tank1Turn)
    {
        sf::Vector2f spawnPos = _t1->getPos() + sf::Vector2f(0, -150);
        sf::Vector2f initVel = _t1->getInitVel();
        cout << endl;
        _p = new Projectile(spawnPos, initVel);
        tank1Turn = false;
        tank1Firing = true;
        mousePressed = false;
    }
    if (mousePressed && tank2Turn) {
        sf::Vector2f spawnPos = _t2->getPos()+sf::Vector2f(0,-150);
        sf::Vector2f initVel = _t2->getInitVel();
        _p = new Projectile(spawnPos, initVel);
        tank2Turn = false;
        tank2Firing = true;
        mousePressed = false;
    }
    if (tank1Turn && moved) {
        _t1->aim(mousePos);
        moved = false;
        mousePressed = false;
    }
    if (tank2Turn && moved) {
        _t2->aim(mousePos);
         moved = false;
        mousePressed = false;
    }
}

Upvotes: 0

Views: 1486

Answers (1)

user3881815
user3881815

Reputation: 103

Consider this a comment. This isn't an answer (I don't have enough reputation to comment so I have to make this an answer) but here are some things I noticed:

  • You should either replace the ifs in your event loop with if-elses, or use a switch

  • sf::Keyboard::isKeyPressed is for real time input (put it inside your game update loop). In your event loop it should be sf::Event::KeyPressed and check which key it was with evt.key.code

  • You aren't checking which mouse button was pressed. Do it with evt.mouseButton.button (sf::Mouse::Button::Left for the left mouse)

  • Of what ive learned is that you should not execute the functions in the while poll event, its only for registering what has happend most recently,so i put them outside instead.

Where did you read that? I don't think that's true

  • There shouldn't just be an array of ifs below the event loop. Code should be structured better

Upvotes: 1

Related Questions