Loading...
Logo
Processing Forum
Hi,
I looked at a lot of timer solutions in the forum, but they don't seem to 'fit'  my problem.  Perhaps you can help?

I'm trying to get a sketch working where the ball starts moving horizontally at a given digital clock-time - say 9.00 ( but for the purpose of the sketch, I set it only to seconds)  pauses for a few seconds, than drops vertically.
It seems to work, but I notice that the DISPLAY_DURATION is shorter than the expected 2 seconds. Also I'm not sure about the function of interval here. It does influence, but in an odd way.
What's wrong and how can it be solved?

many thanks for your time and thought
cheers
nanette

Clock clock;
 //---------------------------
void setup() {
  size(200, 200);
  smooth();
  clock = new Clock ();
}
 //---------------------------
void draw () {
  background (255);
  clock.display ();
  clock.timeDisplay();
  clock.moveBall();
}
//---------------------------
//---------------------------
class Clock {

  int hour;
  int minute;
  int second;

  int x, y;
  int speedX= 2;
  int speedY= 2;

  int startTime; // The (last) time when the mouse have been displayed
  int DISPLAY_DURATION = 2000; // in milliseconds = 1s

  float interval=1000;

  //---------------------------
  Clock () {
    startTime= 0;
  }
  //---------------------------

  void display () {
    hour = hour(); //Get the current time
    minute = minute();
    second = second();
    hour = hour % 24; //Wrap the hour around to conform with 12-hour time
    if (hour == 0) hour = 24; //Make zero-o-clock twelve-o-clock
    backgroundChange ();
  }

  //---------------------------
  void timeDisplay () {
    if (second >=0 && second <30) {
      fill (0);
      textAlign (CENTER, BOTTOM);
      textSize (15);
      text ("Current Time", width/2, height/2);
      textAlign (CENTER, TOP);
      text (hour + ":" + minute + ":" + second + " ", width/2, height/2);
    }
    else 
    {
      fill (255);
      textAlign (CENTER, BOTTOM);
      textSize (15);
      text ("Current Time", width/2, height/2);
      textAlign (CENTER, TOP);
      text (hour + ":" + minute + ":" + second + " ", width/2, height/2);
    }
  }
  //---------------------------
  void backgroundChange () { // at a given time color change happens
    fill (0, (second*4.25));
    noStroke();
    rect (20, 20, width-40, height-40);
  }

  //---------------------------
  void moveBall()// at a stated second the ball starts moving to x=100 then stops
  {

    if (( second >= 10)&& (x<=100)) {
      speedX=2;
      x= x+speedX;
      y=20;
      fill (150);
      ellipse ( x, y, 20, 20); // ball
    }
    else
    {
      speedX=0;
      fill (0);
      ellipse ( x, y, 20, 20); // ball
    }

    if (x>=100) { // I want it to stop for 3 seconds
      startTime= 0;
      if (millis() - startTime > DISPLAY_DURATION)
      {

        fill (0);
        ellipse ( x, y, 20, 20); // ball
        speedY=2;
        y=y+speedY;
      }
    }
  }
}// final bracket

Replies(7)

Have you tried using ijeomamotion's Sequence class? You can do this with less code without having to create a separate "clock" class for every sequence/animation. you can also switch between millisecond and frame time modes/units. by default it uses frames.

Copy code
  1. import ijeoma.motion.tween.*; 
  2. import ijeoma.motion.*; 

  3. Sequence sequence;
  4. PVector position;

  5. void setup() {
  6.   size(200, 200);

  7.   Motion.setup(this);

  8.   position = new PVector(25,25);
  9.   
  10.   sequence = new Sequence()
  11.   .add(new Tween(100).addVector(position, new PVector(100,25)))
  12.   .add(new Tween(100).addVector(position, new PVector(100,100)))
  13.   .play();
  14. }

  15. void draw() {
  16.   background(255);
  17.   stroke(0);
  18.   fill(0); 
  19.   ellipse(position.x, position.y, 10, 10);
  20. }

  21. void keyPressed() {
  22.   sequence.play();
  23. }
this is very interesting to explore- I want to do more animation type stuff. I haven't looked into it, but can it then also do 'pause'- stop start stuff? And it will also work with arrays I presume
thanks
Yes you can play, stop, pause, resume and also seek... download it and check out the examples and getting started here https://github.com/ekeneijeoma/ijeomamotion
I believe going 'the vector way' is interesting. Still I'm wondering why I can't get the intervals right here, 
Ultimatly I'm gping to replace the ball with an image loop, so using interval can be handy here. Any idea?

nanetteh:

Here's one way to organise your code....


When your ball gets to x=100 set a Boolean flag to indicate is has arrived and work out when you want it to drop ie current millis() + 3000, something like this:

if(x>=100 && !flagArrived){
       flagArrived=true;
       nDropTime = millis() + 3000;
}


Then check the flag at each pass through the loop to see if it's time for the ball to drop, if so set another flag...

if(flagArrived){

      if(millis()>=nDropTime) flagDrop=true;
}

then check for dropping...

if(flagDrop){
     
      y++;

}

Basically it's just a case of setting one or more flags and activation times for each event.

Personally, to avoid tangled logic I'd probably use a 'state' for the ball instead

final int GOING_RIGHT = 1
final int WAITING = 2
final int FALLING = 3

then use a switch

switch (ballState){

case GOING RIGHT:
x+=1;
break;

case WAITING:
...
...

case FALLING:
y+=1;
}


Note, I've just typed this in the thread so there may be some typo errors! :D


many thanks, I thought it would be along this line, but couldn't think of how to crack it.
Will try!