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

Author Topic: Problem with checking if left mouse button is pressed  (Read 3026 times)

0 Members and 1 Guest are viewing this topic.

magneonx

  • Full Member
  • ***
  • Posts: 141
    • MSN Messenger - magnumneon04@hotmail.com
    • View Profile
Problem with checking if left mouse button is pressed
« on: June 25, 2013, 06:52:26 pm »
Hello I need help regarding my problem.

We know that there are 2 ways on how to listen if a mouse button was pressed:

1st you check it using poll event
2nd you can use the sf::Mouse::isButtonPressed or something like that

If you will ask, I have a sprite that fires a machine gun. So it had to be holding the button down and it fires rapidly. I also have bullet sprite stored inside vector so I can manage it easily.

One thing I have notice is that if I click the mouse button, just ONE click, it had created 70+ pointers for my bullet sprite.

I have tried the poll event. It works great!! 1 bullet per one click, but I cant hold the mouse button.

Is there anyway I could achieve my desired behavior? SHould I check for the rate of fire?
I need a gun that fires either you click the button or you hold it.

Thanks!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Problem with checking if left mouse button is pressed
« Reply #1 on: June 25, 2013, 07:40:59 pm »
When the button is pressed, instead of adding bullets at every iteration of your logic loop, do it at a fixed/limited interval instead.
Laurent Gomila - SFML developer

magneonx

  • Full Member
  • ***
  • Posts: 141
    • MSN Messenger - magnumneon04@hotmail.com
    • View Profile
Re: Problem with checking if left mouse button is pressed
« Reply #2 on: June 25, 2013, 08:07:29 pm »
Wow, what an honor!

Thanks Laurent.

What have I done is combine the sf::isButtonPressed() inside the poll event loop. It achieve what I wanted. Now it fires like rail gun yeah! I havent done the fire rate logic and what I am doing, I pretty much know this is a bad idea.

The behavior of that is it only fires when I move the sprite or move the mouse, but if I stayed on one direction, it fire on intervals. I don't know how it happens. I haven't try the interval yet and mathematical concept behind it.

So I might open another topic for that one too.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Problem with checking if left mouse button is pressed
« Reply #3 on: June 25, 2013, 08:49:43 pm »
Quote
I pretty much know this is a bad idea.
Indeed :P
Laurent Gomila - SFML developer

 

anything