My translation skills are not good. So I will summarize my problem again. Thank you for the answer. If you want to find a solution to the problem, you can help me with an edit on the code. My problem is that if we click the mouse, the position of the circle that randomly changes position does not detect that we click on it if it is in the position immediately after the previous one. I hope I explained.
I solved the problem and refactored the code so u will have an example of good practices. Maybe u're a beginner, but u will see that an organized code is suggestive and tells u by itself how to optimize and debug. Make sure to read every comment (if possible copy the code in an editor, don't read there)
// Cleaned the headers
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace std;
using namespace sf;
int main() {
// In this block comes the bug fix, please first accustome to the rest of the code, run it, and when u're done uncomment the line below
// srand(time(nullptr)); // Maybe u would like to edit the line so u don't get the the warning when compiling
// There was no randomness because the seed was not set. Permanent random sequence started with 8 9 9 1
// U may consider to get random positions until it is not equal to the previous one, so u don't get repeated ones
sf::RenderWindow window({400, 400}, "beta");
window.setFramerateLimit(60); // Don't burn ur GPU
sf::CircleShape slot(10);
auto emptySlotColor = Color::Green;
slot.setFillColor(emptySlotColor);
slot.setOrigin(7, 10);
CircleShape spinSprite(10);
spinSprite.setFillColor(Color::Red);
spinSprite.setOrigin(7, 10);
vector<sf::CircleShape> slots;
for(auto& pos : vector<Vector2f> {
{205, 40},
{286, 62},
{347, 123},
{369, 205},
{348, 287},
{286, 347},
{205, 369},
{122, 347},
{62, 286},
{41, 206},
{64, 124},
{124, 64}
}) {
slot.setPosition(pos);
slots.push_back(slot);
}
int markedCursor = 0;
int spinCursor = 0;
float switchEvery = 1; // Secs
auto switchSlot = [&] { // I'm using a lambda so I won't change the vars arrangement, so u can recognize the code and compare it. A strong typed fct would've been better
markedCursor = rand() % (slots.size() - 1); // Make the max number dependent, so if u change the number of them u don't have to update here
cout << markedCursor << endl;
slots[markedCursor].setFillColor(Color::Cyan);
};
switchSlot();
spinSprite.setPosition(slots[0].getPosition());
Clock clock;
Event event;
while (window.isOpen()) {
// First time poll events before changing states
while (window.pollEvent(event))
if (event.type == sf::Event::Closed)
window.close();
else if(event.type == Event::MouseButtonPressed)
// This is the right way to check events
// If the button is left but pos differents, the code will do a useless check for right. Chosed this design for readability
if(event.mouseButton.button == Mouse::Left && spinSprite.getPosition() == slots[markedCursor].getPosition()) { // Vector2f has == operator
slots[markedCursor].setFillColor(emptySlotColor); // If u use the same value in 2 locations, use a var (const how people prefer)
// No need to store a int (it should've been a bool), do the stuff right away
// It wouldn't work with previous event handling methods
switchSlot();
}
else if(event.mouseButton.button == Mouse::Right) {
switchEvery -= (switchEvery < .1f ? .01f : .1f); // One line
cout << switchEvery << endl;
}
window.clear();
// Do the check in one line. Good formatted code doesn't need (in general) explications
if (clock.getElapsedTime().asSeconds() >= switchEvery) { // I'm quite sure u want >= rather than >
clock.restart();
spinCursor = (spinCursor == 11 ? 0 : spinCursor + 1); // One line, easier to read
// Don't follow this with another "for" loop. U already have everything u need
// Don't do every frame either, u wanna change the pos only when u're in this "if"
spinSprite.setPosition(slots[spinCursor].getPosition()); // Interchanging positions doesn't need to explicit both x and y
}
for(auto& slot : slots)
window.draw(slot);
window.draw(spinSprite); // U should use same technique as the cyan circle
window.display();
}
}