Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Clickable Sprite  (Read 2777 times)

0 Members and 1 Guest are viewing this topic.

newbie443

  • Newbie
  • *
  • Posts: 1
    • View Profile
Clickable Sprite
« on: September 30, 2015, 12:59:55 am »
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 :)

DarkRoku12

  • Full Member
  • ***
  • Posts: 203
  • Lua coder.
    • View Profile
    • Email
Re: Clickable Sprite
« Reply #1 on: September 30, 2015, 05:24:00 am »
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:
Quote
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 ) )
 
I would like a spanish/latin community...
Problems building for Android? Look here

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Clickable Sprite
« Reply #2 on: October 05, 2015, 12:59:25 am »
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.
« Last Edit: October 12, 2015, 05:01:46 pm by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: Clickable Sprite
« Reply #3 on: October 05, 2015, 05:45:07 am »
He doesnt? One checks s_startGame and the other checks s_exitGame.

 

anything