timed events - trigger

edited November 2014 in Questions about Code

taking beginning online course, so some of this basic stuff is a little hard for old guy... trying to write program or actually modify one - to run three different circles with different y coordinate across the screen with the time triggering each event. I know each event needs to be in its own set of brackets and if/else statements, although I set the int initially. Can do it with mousePressed, but having a bear of a time with timer. Changed the int for the time in ms, know I have to write the code into different areas, but that's the rub. When I run code in the brackets I thought I should use, the program gives me an "}" error. Erased some of the code but can use some pointer.

int time1 = 2000;

int time2 = 5000;

int time3 = 10000;

float x = 0;

float y = 0;

void setup()
 {

  size(480, 500);

  smooth();

}


void draw() 

{

  int currentTime = millis();

  background(204);

  if (currentTime > time2) 


  {

    x += 0.5;

  } 

  else if (currentTime > time1) 

  {

   x += 2;

  }

  ellipse(x, 60, 90, 90);

  {

    else if (currentTime > time2)

  }

    x+=2;

}

ellipse(x, y+200, 90, 90);

  {



 // ellipse (x, y+400, 90,80);

}

original code example - but it stops the circle at the time specified, I need it going across the screen, 3 different circles or three different times and levels. Can use a little educating? Thanks

int time1 = 2000;

int time2 = 4000;

float x = 0;


void setup()

 {

  size(480, 120);

  smooth();

}


void draw() 

{

  int currentTime = millis();

  background(204);

  if (currentTime > time2)

 {

    x -= 0.5;

  } else if (currentTime > time1)

 {

    x += 2;

  }

  ellipse(x, 60, 90, 90);


}
Tagged:

Answers

  • To format the code on this forum highlight it and press Ctrl + K

    This article is not exactly what you need but it might give you some ideas.

  • thanks will take a look. my old brain cells and the "{" "} edited and 'ctrl K' thanks!

  • edited November 2014

    Close, in a way, excpet need to send one ball across the screen, then it disppears or reappears lower on the y axis, then same thing for a third time. May be able to use a couple ideas in the code. The on line instructor usually posts some videos on the topic, but not for this one. There's no more room in the in-person class, so having to wing it online.

  • Indeed, you got lost on the third comparison. The syntax is regular: that's not the else if you must put between braces, but the code depending on this comparison. And they must be continuous, ie. you cannot have a call to ellipse() before the else.

    if (c1)
    {
      x += ...
    }
    else if (c2)
    {
      x += ...
    }
    else if (c3)
    {
      x += ...
    }
    elllipse();
    

    (c_n_ is a conditional, of course).

    Of course, you can put a call to ellipse() inside the braces too, if it is what is needed (different coordinates, etc.).

Sign In or Register to comment.