Hey Guys,
im just starting out and im having a little bit of trouble.
Im making a Pong clone but im already failing at the Menu .
The problem is that if i want to exit/play the game i have to click several times while moving the mouse(if i dont move the mouse it wont react) but i want to one-click without movement to start/exit the game.
Menu::menuState Menu::showMenu(sf::RenderWindow &window)
{
sf::Texture pong;
sf::Texture startGame;
sf::Texture exitGame;
pong.loadFromFile("images/pongScreen.png");
startGame.loadFromFile("images/startGame.png");
exitGame.loadFromFile("images/exitGame.png");
sf::Sprite s_pong(pong);
sf::Sprite s_startGame(startGame);
sf::Sprite s_exitGame(exitGame);
s_pong.setPosition(200, 60);
s_startGame.setPosition(280, 225);
s_exitGame.setPosition(280, 275);
window.draw(s_pong);
window.draw(s_startGame);
window.draw(s_exitGame);
window.display();
sf::Event currentEvent;
while (window.pollEvent(currentEvent))
{
if (currentEvent.type == sf::Event::MouseButtonPressed)
{
if (currentEvent.mouseButton.button == sf::Mouse::Left)
{
if (sf::Mouse::getPosition(window).y > s_startGame.getGlobalBounds().top &&
sf::Mouse::getPosition(window).y < s_startGame.getGlobalBounds().height + s_startGame.getGlobalBounds().top &&
sf::Mouse::getPosition(window).x < s_startGame.getGlobalBounds().width + s_startGame.getGlobalBounds().left &&
sf::Mouse::getPosition(window).x > s_startGame.getGlobalBounds().left)
{
return playing;
}
if (sf::Mouse::getPosition(window).y > s_exitGame.getGlobalBounds().top &&
sf::Mouse::getPosition(window).y < s_exitGame.getGlobalBounds().height + s_exitGame.getGlobalBounds().top &&
sf::Mouse::getPosition(window).x < s_exitGame.getGlobalBounds().width + s_exitGame.getGlobalBounds().left &&
sf::Mouse::getPosition(window).x > s_exitGame.getGlobalBounds().left)
{
return exiting;
}
}
}
}
return wait;
}
im using SFML 2.1 and visual studio.
thanks in advance :)
Try this example.
#include <iostream>
#include <SFML\Graphics.hpp>
#include <vector>
using namespace sf ;
using namespace std ;
int main( int argc, char* argv[] )
{
bool exit = false ;
std::string Action ;
RenderWindow window( VideoMode( 800 , 600 ) , "myWindow" ) ;
window.setActive( false ) ;
window.setFramerateLimit( 65 ) ;
RectangleShape s_startGame( Vector2f( 300 , 100 ) ) ;
s_startGame.setFillColor( Color::Red ) ;
s_startGame.setPosition( 280 , 225 ) ;
while ( window.isOpen() )
{
Event event ;
while ( window.pollEvent( event ) )
{
if ( event.type == Event::Closed )
window.close() ;
if ( event.type == Event::MouseButtonPressed )
{
if ( event.mouseButton.button == sf::Mouse::Left )
{
Vector2i MousePos = Mouse::getPosition( window ) ;
FloatRect objPos = s_startGame.getGlobalBounds() ;
if ( objPos.contains( MousePos.x , MousePos.y ) )
{
if( exit )
{ exit = false ; Action = "Exit" ; }
else
{ exit = true ; Action = "Enter" ; }
cout << "Touched by mouse, action: " << Action << endl ;
}
}
}
}
window.clear() ;
window.draw( s_startGame ) ;
window.display() ;
}
return 0 ;
}
Please don't do that:
if (sf::Mouse::getPosition(window).y > s_startGame.getGlobalBounds().top &&
sf::Mouse::getPosition(window).y < s_startGame.getGlobalBounds().height + s_startGame.getGlobalBounds().top &&
sf::Mouse::getPosition(window).x < s_startGame.getGlobalBounds().width + s_startGame.getGlobalBounds().left &&
sf::Mouse::getPosition(window).x > s_startGame.getGlobalBounds().left)
Instead store the result in a variable and use the x,y / top,left,width,height properties
Vector2i mousePos = sf::Mouse::getPosition(window) ;
doWhatEver( mousePos.x , mousePos.y ) ;
And use the built-in method to check if a point intersect or contain.
FloatRect objRect = s_startGame.getGlobalBounds() ;
// objRect.contains( x , y )
// objRect.intersects( FloatRect( x , y , width , height ) )
Why do you have two identical conditions following each other?
bool theSameThing = true;
if (theSameThing)
{
return playing;
}
if (theSameThing) // this will never be checked when it is true
{
return exiting;
}
will never return exiting since if the condition it requires is true, it will already have returned playing.
EDIT: My bad. As Gambit points out in the following comment, I'd made an error.