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

Author Topic: Way to detect how many times the mouse has been pressed?  (Read 2572 times)

0 Members and 1 Guest are viewing this topic.

MaeZer

  • Newbie
  • *
  • Posts: 9
    • View Profile
Way to detect how many times the mouse has been pressed?
« on: June 01, 2015, 07:37:01 pm »
I would like to know how one would somehow remove (well, set it's colour transparent) a rec shape, one by one, each time the mouse is clicked. So the first time you click the mouse, it removes the first shape, then when you click it again, it removes the second one, and so on. Perhaps there is a simple solution to this, I'm not sure... It's quite a basic problem with probably an easy fix, but I'm fairly new to this  8)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Way to detect how many times the mouse has been pressed?
« Reply #1 on: June 01, 2015, 08:12:05 pm »
You should probably read the tutorial about SFML, and if you don't understand it yet, invest a bit more time in your C++ book ;)

This is a really basic problem that should be easily solvable after learning SFML. With your current description, we can't help you much other than giving you the code that does this (and at least I won't do that) -- so maybe you can describe a bit more concretely where you're stuck?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

MaeZer

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Way to detect how many times the mouse has been pressed?
« Reply #2 on: June 01, 2015, 10:19:20 pm »
I tried this but it doesn't work and I don't know why, where's the problem in my code? The increment operators?



                        int bullm = 0;
                        int x = 1;
                       
                        if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
                        {
                                bullm = +x;
                        }
                       


                        if (bullm == 0)
                        {
                               
                                bull1.setFillColor(sf::Color::Yellow);
                                bull2.setFillColor(sf::Color::Yellow);
                                bull3.setFillColor(sf::Color::Yellow);
                                bull4.setFillColor(sf::Color::Yellow);
                        }
                        if (bullm == 1)
                        {
                               
                                bull1.setFillColor(sf::Color::Yellow);
                                bull2.setFillColor(sf::Color::Yellow);
                                bull3.setFillColor(sf::Color::Yellow);
                                bull4.setFillColor(sf::Color::Transparent);
                        }
                        if (bullm == 2)
                        {
                                bull1.setFillColor(sf::Color::Yellow);
                                bull2.setFillColor(sf::Color::Yellow);
                                bull3.setFillColor(sf::Color::Transparent);
                                bull4.setFillColor(sf::Color::Transparent);
                        }
                        if (bullm == 3)
                        {
                                bull1.setFillColor(sf::Color::Yellow);
                                bull2.setFillColor(sf::Color::Transparent);
                                bull3.setFillColor(sf::Color::Transparent);
                                bull4.setFillColor(sf::Color::Transparent);
                        }
                        if (bullm == 4)
                        {
                                bull1.setFillColor(sf::Color::Transparent);
                                bull2.setFillColor(sf::Color::Transparent);
                                bull3.setFillColor(sf::Color::Transparent);
                                bull4.setFillColor(sf::Color::Transparent);
                        }
 

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Basic C++
« Reply #3 on: June 01, 2015, 10:29:09 pm »
The syntax to add something (b) to itself (a) is:
a = a + b
or
a += b

If the value you want to add is 1 you can also write
++a
or
a++
It's the same as
a = a + 1
or
a += 1

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Way to detect how many times the mouse has been pressed?
« Reply #4 on: June 01, 2015, 10:35:43 pm »
Consider what happens when the user holds down the button - it'll report being pressed every time you test (which can easily be many, many times each second). If you only want to do something on each click, consider using events instead.
Also, as G. said, you need to learn your basic C++ syntax...

Edit: additionally, since 'bullm' can only take on one value at a time, your consecutive 'if's would be more efficient (and logical) as either a 'switch' or a 'if' followed by a number of 'else if's. Or you could simplify by just doing two loops; the first would do '4 - bullm' instances of yellow and the next would do 'bullm' instances of transparent (assuming you cap bullm at 4) - a lot shorter (you'd need to store the objects to operate on in a container (like vector) instead of seperate variables, but that would probably be a good change regardless)...
« Last Edit: June 01, 2015, 10:57:32 pm by Jesper Juhl »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Way to detect how many times the mouse has been pressed?
« Reply #5 on: June 02, 2015, 02:13:10 am »
If what you mean by "not working" is that bull4 goes transparent while the mouse button is held down and comes back when released, the problem is your increment. G's solution should help correct that. Some information that you may find interesting.

Just so you know why your increment code is incorrect:
bullm = +x;
effectively just means bullm = x;
Since x is always 1 at that point, it means bullm = 1;

It also looks like you're resetting bullm (to zero) in every frame, directly before attempting to increment it. It needs to be set only once - outside the main loop - and then incremented using the correct code.

p.s. if you do make these corrections, your variable will increment every frame because you're testing the mouse every frame. Consider events as Jesper already mentioned  ;)
« Last Edit: June 02, 2015, 02:14:43 am by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

MaeZer

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Way to detect how many times the mouse has been pressed?
« Reply #6 on: June 02, 2015, 06:27:38 pm »
Thanks, I had to change the increments and use events in case another newbie had this problem. Appreciate the patience  ;)

 

anything