Advertisement

[SFML] Mouseclick & Release repeats?

Started by March 07, 2015 01:28 PM
6 comments, last by Elit3d 9 years, 10 months ago

Here is a gif of what is happening: here

Basically, when I click the button and i release the mouse, it just repeats over and over until it decides to stop. I want to be able to click it and it show one number only. here is my code for clicking the button:


		if (event.type == event.MouseButtonReleased && event.mouseButton.button == sf::Mouse::Left)
		{
			//If we clicked within the exit rectangle, then exit!
			if (shape.getGlobalBounds().contains(event.mouseButton.x, event.mouseButton.y) == true)
			{
				//Exit the game.
				i1.setIncrease(i1.getIncrease() + 1);
				std::cout << i1.getIncrease() << std::endl;
			}

		}

Seems like it spams it till I move my mouse

Advertisement

Show a full example (all includes, main, event loop, etc.) that compiles and reproduces the problem for you, there are many errros in event loop code that could cause behaviour like that.

Show a full example (all includes, main, event loop, etc.) that compiles and reproduces the problem for you, there are many errros in event loop code that could cause behaviour like that.

No errors. I know the problem lies within the code I posted. I read in the documentation that if I add: window.setKeyRepeatEnabled(false); it will stop that but it hasn't which is odd. Also for the record, the same numbers display in the console so if I did std::cout << "Test"; that would spam test a bunch of times too till I move my mouse then it stops

I can't compile and test a piece of code with single if statement like that though... smile.png so if you have a ready example you might as well give it instead of hoping I'll reproduce the bug myself.

Key repeat only affects keyboard keys, not mouse buttons.

I can't compile and test a piece of code with single if statement like that though... smile.png so if you have a ready example you might as well give it instead of hoping I'll reproduce the bug myself.

Key repeat only affects keyboard keys, not mouse buttons.

main.cpp


#include <SFML/Graphics.hpp>
#include <iostream>
#include <sstream>
#include "increase.h"


int main()
{
	sf::RenderWindow window(sf::VideoMode(800, 800), "SFML works!");
	sf::CircleShape shape(20.0f);
	shape.setFillColor(sf::Color::Green);
	window.setKeyRepeatEnabled(false);

	increase i1;

	sf::Font font;
	font.loadFromFile("font/arial.ttf");

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

		if (sf::Mouse::isButtonPressed(sf::Mouse::Right)){
			int t = 0;
			t + 1;
			std::cout << t;

		}

		if (event.type == event.MouseButtonReleased && event.mouseButton.button == sf::Mouse::Left)
		{
			if (shape.getGlobalBounds().contains(event.mouseButton.x, event.mouseButton.y) == true)
			{
				i1.setIncrease(i1.getIncrease() + 1);
				std::cout << "test" << std::endl;
				
			}

		}

		sf::Text roomDisplay("", font);
		roomDisplay.setCharacterSize(20);
		roomDisplay.setColor(sf::Color::White);
		roomDisplay.setPosition(100, 100);
		std::stringstream ss;
		ss << i1.getIncrease();
		roomDisplay.setString(ss.str().c_str());

		window.clear();
		//Draw Stuff

		window.draw(shape);
		window.draw(roomDisplay);
		//End Draw Stuff
		window.display();
	}

	return 0;
}

increase.h


#pragma once
class increase
{
public:
	increase();
	~increase();

	int getIncrease();
	void setIncrease(int increase);

private:
	int m_increase;
};

increase.cpp


#include "increase.h"


increase::increase()
{
	m_increase = 0;
}


increase::~increase()
{
}

int increase::getIncrease(){
	return m_increase;
}

void increase::setIncrease(int increase){
	m_increase = increase;
}

Advertisement

The way you wrote it (and the way C++ stack works and because sf::Event has no constructor), the event variable will always have last event from pollEvent in it and you then use it over and over again, you got a single mouse release event but because you don't check if pollEvent returned any new events, you use it many times, this is why usual advice is to use events only in while(pollEvent()){} block.

The way you wrote it (and the way C++ stack works and because sf::Event has no constructor), the event variable will always have last event from pollEvent in it and you then use it over and over again, you got a single mouse release event but because you don't check if pollEvent returned any new events, you use it many times, this is why usual advice is to use events only in while(pollEvent()){} block.

Oh I see, totally overlooked that! Many thanks :) +rep

This topic is closed to new replies.

Advertisement