r/sfml • u/TheEyebal • 14d ago
Expecting primary expression error
I keep getting the error error: expected primary expression before 'mouse' and error: expected primary expression before 'rect'
Can someone assist me on this because I am lost
CONTEXT
I made a boolean function that detects if a point is colliding with a rectangle and in my main function when button is pressed and mouse is colliding with rectangle than the console needs to output string
bool pointRectCollision(Mouse mouse, RectangleShape rect){
if(mouse.getPosition().x > int(rect.getPosition().x) && mouse.getPosition.x() < int(rect.getPosition().x + rect.getSize().x) && mouse.getPosition().y > int(rect.getPosition().y) && mouse.getPosition.y() < int(rect.getPosition().y + rect.getSize().y)){
return true;
}
else {
return false;
}
}
int main(){
// GAME LOOP
if (mouse.isButtonPressed(Button::Left) && pointRectCollision(mouse, pauseBtn)){
cout << "COLLISION" << endl;
}
return 0;
}UPDATE
I was able to fix the problem. I had to fix the syntax of how I was declaring mouse and also nest my conditional statement for detecting collision.
// function to detect point-rectangle collision
bool pointRectCollision(Vector2i mousePos, RectangleShape &rect){
if(mousePos.x > int(rect.getPosition().x) && mousePos.x < int(rect.getPosition().x + rect.getSize().x) && mousePos.y > int(rect.getPosition().y) && mousePos.y < int(rect.getPosition().y + rect.getSize().y)){
return true;
}
else {
return false;
}
}
int main(){
// GAME LOOP
// MOUSE
if (Mouse::isButtonPressed(Mouse::Button::Left)){
if(pointRectCollision(Mouse::getPosition(window), pauseBtn)){
cout << "collision" << endl;
}
}
}1
u/landmvx 14d ago
Another Option ist to use the Rects contains method, to detect if the point is within the rectangle: https://www.sfml-dev.org/documentation/3.1.0/classsf_1_1Rect.html
1
u/TheEyebal 13d ago
Ok I will look at that. I am new at SFML and was trying to find something that contains the button
1
u/thedaian 14d ago
Is the error on the line with the pointRectCollision(
If so, you're probably declaring it before you have using namespace sf;
Which is another reason to avoid using namespace sf; and just typing out sf::