Reputation: 8280
I'm stuck on working out the correct syntax to pass the mouseX and mouseY position to my class.
My attempt was like this:
// Start the game loop
while (window.isOpen())
{
// Process events
sf::Event event;
while (window.pollEvent(event))
{
// Close window : exit
if (event.type == sf::Event::Closed) {
window.close();
}
}
// Clear screen
window.clear();
// Draw the sprite
window.draw(sprite);
window.draw(lsprite);
if(btn_quit.IsIn(sf::Event::MouseMoveEvent::x,sf::Event::MouseMoveEvent::y){
btn_quit.RenderImg(window,"button_on.png");
} else {
btn_quit.RenderImg(window,"button.png");
}
///rest of the code not relevant to this issue
This is in my class:
bool IsIn( int mouseX, int mouseY )
{
if (((mouseX > m_x) && (mouseX < m_x + m_w))
&& ((mouseY > m_y) && (mouseY < m_y + m_h ) ) ) {
return true;
} else {
return false;
}
}
I keep getting an error with the inputs of x and y how ever which says this:
error C2597: illegal reference to non-static member 'sf::Event::MouseMoveEvent::x'
error C2597: illegal reference to non-static member 'sf::Event::MouseMoveEvent::y'
Upvotes: 0
Views: 2281
Reputation: 17176
What you're currently doing is incorrect in a few ways. First of all you can't access x and y as static data like that, it simply does not exist.
Next to that, SFML events are essentially unions, meaning their actual contents will depend on the type of event you try to handle. You cannot get an x and y value for every event, not from the event object at least. If you have a mouse event you'll do something like this:
if(event.type == sf::Event::MouseMoved)
{
// do something with event.mouseMove.x and event.mouseMove.y
}
Alternatively, if you want to do this outside a mouseMoved
event scope, you can always use the Mouse
class. This contains a getPosition
method that will return a 2d vector containing x
and y
:
sf::Mouse::getPosition(); //Absolute coordinates
sf::Mouse::getPosition(window); //Relative to window
Upvotes: 1
Reputation: 15313
You're trying to statically access non-static member fields of a struct. You can't do that. Try something like this:
// Start the game loop
while (window.isOpen())
{
// Process events
sf::Event event;
while (window.pollEvent(event))
{
// Close window : exit
if (event.type == sf::Event::Closed) {
window.close();
}
else if (event.type == sf::Event::MouseMove)
{
if(btn_quit.IsIn(event.MouseMoveEvent.x, event.MouseMoveEvent.x){
btn_quit.RenderImg(window,"button_on.png");
} else {
btn_quit.RenderImg(window,"button.png");
}
}
}
// Clear screen
window.clear();
// Draw the sprite
window.draw(sprite);
window.draw(lsprite);
Upvotes: 3