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

Author Topic: Problem with handling events.  (Read 5425 times)

0 Members and 1 Guest are viewing this topic.

darekg11

  • Full Member
  • ***
  • Posts: 127
    • View Profile
Problem with handling events.
« on: October 03, 2010, 09:24:14 pm »
Okey, I need something like this:
When You click at the area of string then program starts OGG file and after 3 seconds quit.
I already spent 3hours trying to get it to work.
Event.type == sfvEventMouseButtonPressed and sfvEventMouseButtonRealeased can't do it because then I have to hold a mouse at string for 3 seconds to quit app.

I need something that after 1x click start clock, play music and after 3 seconds quit app.

Please give me advice/tips whatever.
I tryed to get Input of window and then use sfMouseButtonEvent and sfButtonX1 but I don't know excatly how to use it.

Do I need place that code in Event loop or in App.IsRunning loop?
Which functions and types of events I need to use?

Really guys anything, I will keep on trying do this but help would be really needed.

Canadadry

  • Hero Member
  • *****
  • Posts: 1081
    • View Profile
Problem with handling events.
« Reply #1 on: October 03, 2010, 10:59:09 pm »
You need a bool too store whether to mouse has been clicked or not. Then, when the  sfvEventMouseButtonPressed is raised ( or buttonRelease whatever) start then clock and the music.

In each loop turn you will check if your bool is true and if the timer is correct (<3s)  if not stop the music. Reset the timer and set the bool to false.

darekg11

  • Full Member
  • ***
  • Posts: 127
    • View Profile
Problem with handling events.
« Reply #2 on: October 04, 2010, 07:46:17 am »
So in Event loop I need to check if sfButtonLeft button is pressed by using function: sfInput_IsMouseButtonDown?
And what then?
Sorry but I kind can't get it.

I will try do something with it, but still help would be needed.

EDIT: I did it with using another thread and then stoping main thread for 1 second and then clossing app but I fell like there is easier ethod to do it like that one mentioned by mooglwy but I can't get his method to work.

Could someone help me with this?

Canadadry

  • Hero Member
  • *****
  • Posts: 1081
    • View Profile
Problem with handling events.
« Reply #3 on: October 04, 2010, 06:44:36 pm »
something like that.

Code: [Select]
[...]
bool keypressed = false;
s::clock timer;
[...]
while(true)
{
    sf::Event Event;
    while (App.GetEvent(Event))
    {
/**/    if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Space))
        {
            if(keypressed) break;
            keypressed = true;
            timer.start();
            playsound();
        }
    }

     if(keypressed && (timer > 3s)
    {
        exit();
    }
};


With this code a music is played when you pressed space and the application quit after 3seconde. You just have to change the line with the trigger event(/**/ line). Is this clearer?

darekg11

  • Full Member
  • ***
  • Posts: 127
    • View Profile
Problem with handling events.
« Reply #4 on: October 04, 2010, 08:52:56 pm »
It's working great, thanks man.
But I am not sure if I understand all lines:

Code: [Select]

if(keypressed) break; If keypressed == TRUE quit, it's just for safe because keypressed at the beginning will always be FALSE right?
keypressed = true; Setting keypressed to TRUE
if(keypressed && (timer > 3s) Checking if keypressed == TRUE and time > 3


Is it right?
Anyway mad PROPS man, really I can't tell You grateful I am.

Canadadry

  • Hero Member
  • *****
  • Posts: 1081
    • View Profile
Problem with handling events.
« Reply #5 on: October 05, 2010, 07:54:59 am »
Quote from: "darekg11"
Code: [Select]

if(keypressed) break; If keypressed == TRUE quit, it's just for safe because keypressed at the beginning will always be FALSE right? it's for avoining to restart the timer and replay the music every loop.
keypressed = true; Setting keypressed to TRUE
if(keypressed && (timer > 3s) Checking if keypressed == TRUE and time > 3. yes completly right


Is it right?
Anyway mad PROPS man, really I can't tell You grateful I am.


You're wellcome.  :wink:

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
Problem with handling events.
« Reply #6 on: October 05, 2010, 12:50:00 pm »
Quote from: "darekg11"
If keypressed == TRUE quit, it's just for safe because keypressed at the beginning will always be FALSE right?

It's just to avoid multiple keypress ;)
Mindiell
----

darekg11

  • Full Member
  • ***
  • Posts: 127
    • View Profile
Problem with handling events.
« Reply #7 on: October 05, 2010, 02:48:23 pm »
Ahh I see, thanks guys.

 

anything