Loading...
Logo
Processing Forum
Hi Forum, I'm having a mental block with how to deal with a timer issue...

I have an array of objects (circles) that I am testing to see if the mouse is positioned over.
I also have a timer that will trigger a function after the mouse stays over an object for a certain amount of time.

My problem is that if I mouse over a circle, the time overtop of it is additive, as it's only resetting the timer on a successful trigger call.
If i try to reset the timer when the mouse is not overtop of an object, it's resetting it continually, as I'm running through an array of objects to which some will be equated to false... 

Here's My Code: 

Copy code
  1.   for(int i = 0; i < circle.length; i++) {
  2.     circle[i].update(); 
  3.     if(circle[i].over == true) {
  4.       if(millis() < newTimer + (trigTime * 1000) ) { // trigTime is an int
  5.         // Nothing Here
  6.       }
  7.       else {
  8.         // Send The Trigger Event
  9.         trigger(i);
  10.         // Reset The Timer
  11.         newTimer = millis();
  12.       }
  13.     }
  14.     else {
  15.       // PROBLEM: Can't Figure Out How To Reset Timer For 'i' w/o Resetting All Continually 
  16.       // newTimer = millis(); 
  17.     }
  18.    
  19.   }

Appreciated if anyone sees a strategy to deal with this... 

Thanks in advance, 

~ Jesse

Replies(2)

I've read this a few times, but I feel there are some discrepancies between your description and your code. Both of them drift between handling things at the individual object versus multiple object level. Especially the fact that you have/want a single timer, but you trigger(i) towards the indivual i object's level. For discussion sake the two opposing options. Would any of these work and if not, please elaborate on your goal.

Option 1 - if the mouse is over one or more object for a specific amount of time, trigger something globally
Copy code
  1. boolean arrayMouseOver;

  2. void draw() {
  3.   // set global boolean to false
  4.   arrayMouseOver = false;
  5.   for (int i = 0; i < circle.length; i++) {
  6.     circle[i].update();
  7.     if (circle[i].over) {
  8.       arrayMouseOver = true;
  9.     }
  10.   }
  11.   // over at least one and perhaps more objects
  12.   if (arrayMouseOver) {
  13.     // here the timer value is not updated (see below)
  14.     // after the trigger time has passed...
  15.     // it will be updated and the trigger function will be called
  16.     if (millis() > newTimer + (trigTime * 1000) ) {
  17.       newTimer = millis();
  18.       trigger();
  19.     }
  20.   // not over any object => timer is continuously actualised
  21.   } else {
  22.     newTimer = millis();
  23.   }
  24. }

Option 2 - if the mouse is over this object for a specific amount of time, trigger something in this object
Copy code
  1.   for(int i = 0; i < circle.length; i++) {
  2.     circle[i].update();
  3.     if(circle[i].isOver()) {
  4.       if(millis() > circle[i].newTimer + (trigTime * 1000) ) {
  5.         circle[i].trigger();
  6.         circle[i].newTimer = millis();
  7.       }
  8.     } else {
  9.       circle[i].newTimer = millis();
  10.     }
  11.   }
Hey Amnon, thanks for the reply. Yes, the 2nd Option was what I was looking for exactly... thanks for the help, and sorry for not being more clear... 

cheers, 

~ J