Loading...
Logo
Processing Forum

Switch Case Program

in General Discussion  •  Other  •  1 year ago  
Hi there,

I have to write a switch case program that steps through 9 planned actions with a 5 second delay.  Not sure how to do this/where to start. Any help is greatly appreciated.

Thanks!

Replies(9)

The first step is to point to switch reference...
Then to point to the Technical FAQ, although I fear I never finished the section on having steps in a program. But it has relevant information anyway.
Also search the forum, there was recent discussions using switch for this purpose.
steps backwards to make zero case stops the cicle.
Copy code
  1.     int steps = 9;
       
        stepBystep(steps);

        void stepBystep(int steps)
        {
            while(steps>0)
            {
                switch(steps)
                {
                      case 0:
                            println("Zero");  // never happens, zero breaks the cicle and finishes it
                        break;

                      case 1:
                            println("One");  // code for step 1
                        break;

                      case 2:
                            println("Two");  // code for step 2
                        break;

                    ...
                }
           
                steps--;
            }
        }
for the 5 seconds sleep, you can use sleep(5000), but i think it has to be inside a thread.

EDIT: wrap this stuff inside a thread and it will work ok, don't forget to put the sleep(5000) after the switch and before the while ends.
maxtrix.net
Thanks for the reply.  Im still having difficulties with this. I have this chart below as a guideline as the different steps i want to happen throughout the program. I just dont know how to execute it!



Timer timer; 

int timeSpeed = 3;
int action = 1;
float targetX;
float targetY;
int distTarget;

int dotMood;
float mood;
boolean reset = false;

//DISTANCE
float distance;


void setup () {
  size (700, 700);
  /*targetX = random (0, width);
   targetY = random (0, height);
   timer = new Timer(timeSpeed);
   mood = random (255);
   timer.start ();*/
  smooth();
}

void draw () {
  rectMode (CENTER);
  background (0);

  //void distCheck () {
    distance = dist (mouseX, mouseY, targetX, targetY);
    if (distance <= 375) {
      //fill (0);
      


      // FLAME
      fill (255, 204, 0); //BRIGHT YELLOW
      ellipse(350, 240, 60, 175); 
      // WICK
      fill (255); //WHITE
      rect (350, 340, 5, 25);
      // BASE OF CANDLE
      fill (178, 200, 0); // GREEN
      rect (350, 500, 100, 300);
    }
  }
is that code running?

EDIT: sorry, sure it is, got confused  with some double slashed comments.

so, what's the trouble? do you need to implement those candles actions? are they donne?

maxtrix.net
yeah i just have NO idea how to implement those actions into code
No idea, hum?!

First of all, as far as i can see, the switch case is minor issue here.
I don't quite understand the 9 planned actions with a 5 second delay,
looking to your chart it seems that if:

candle is touched fast, blows out
candle is touched slowly , flicks
....
getting close to the candle fast, flame change color.
... and so on....

is this right? or am i making wrong assumptions?

now back to processing,
in my point of view  you need to

* capture the speed of the mouse, and store it to a variable.
* capture the distance of the mouse, to the target. (probabily the candle. can't see the target coords in your code) and store it.
* draw function of the candle.

* function with the switch case
      if touch fast (1) change candle variables to something (draw function does the rest)
      if touch slow (3) change candle variables to something (draw function does the rest)
      ....


are you able to it?






maxtrix.net
Yeah, your right with the chart. Those things should be happening when the user touches, is near or is far to the candle.

I definitely understand what your saying about the three things i have to do, but really dont know how to program it. Are you able to show me an example if you dont mind?


to have the distance mouse - candle use dist()

to get the speed of the mouse, use dist between mouseX / Y and pmouseX / Y
copy and past

i wrote kind of a simple code just to allow you to feel the thing, if you know what i mean.
i implemented 2 functions,

      candle_changes_color();
      you need to move the mouse left and right, or whatever a little bit freneticly on top of the circle     

      candle_melts();
      you need to move th mouse a centimiter around the circle at medium speed.

just try it.

the rest is up to you, good luck.

Copy code
  1.  
        // setting window size
        float sizeX = 700;
        float sizeY = 700;

        // stuff for the target position
        float targetX;
        float targetY;

        // stuff for the distance
        float distance;
        float distance_option;
        float distance_touch = 50;     // tune up this value
        float distance_close = 100;    // tune up this value

        // stuff fot the speed
        float start_x;
        float start_y;
        float end_x;
        float end_y;
        float speed;
        float speed_option;
        float speed_slow = 50;        // tune up this value
        float speed_medium = 100;    // tune up this value

        // candle stuff
        color candle_c = color(255, 204, 0);
        float candle_w = 100;
        float candle_h = 100;

        void setup ()
        {
            size (parseInt(sizeX), parseInt(sizeY));
            frameRate(10);

            // setting target to the center of the window
            targetX = sizeX/2;
            targetY = sizeY/2;

            smooth();
            loop();
        }

        void draw ()
        {
            calc_distance();
            calc_speed();
            trigger();
            background (0);
            candle();
        }

        void trigger()
        {
            if(distance_option == 1 && speed_option == 1 )
            {
                // if is touching and speed is slow... do stuff
            }
            else if(distance_option == 1 && speed_option == 2 )
            {
                // if is touching and speed is medium... do stuff
            }
            else if(distance_option == 1 && speed_option == 3 )
            {
                // if is touching and speed is fast... do stuff
            }
            else if(distance_option == 2 && speed_option == 1 )
            {
                // if distance is close and speed is slow... do stuff
            }
            else if(distance_option == 2 && speed_option == 2 )
            {
                // if distance is close and speed is medium... do stuff
            }
            else if(distance_option == 2 && speed_option == 3 )
            {
                candle_changes_color();
            }
            else if(distance_option == 3 && speed_option == 1 )
            {

            }
            else if(distance_option == 3 && speed_option == 2 )
            {
                candle_melts();
            }
            else if(distance_option == 3 && speed_option == 3 )
            {

            }
        }

        void candle_melts()
        {
            if(candle_w > 0)
            {
                candle_w--;
                candle_h--;
            }
        }

        void candle_changes_color()
        {
            candle_c = color(random(255), random(255), random(255));
        }

        void calc_distance()
        {
            distance = dist (mouseX, mouseY, targetX, targetY);
            if(distance <= distance_touch)
            {
                distance_option = 1;
            }
            else if(distance > distance_touch && distance <= distance_close)
            {
                distance_option = 2;
            }
            else
            {
                distance_option = 3;
            }
            //println("distance = " + distance_option);
        }

        void calc_speed()
        {
            start_x = end_x;
            start_y = end_y;
            end_x = mouseX;
            end_y = mouseY;
            speed = dist (start_x, start_y, end_x, end_y);

            if(speed <= speed_slow)
            {
                speed_option = 1;
            }
            else if(speed > speed_slow && speed <= speed_medium)
            {
                speed_option = 2;
            }
            else
            {
                speed_option = 3;
            }
            //println("   speed = " + speed_option);
        }   

        void candle()
        {
            fill (candle_c);
            ellipse(targetX, targetY, candle_w, candle_h);
        }

maxtrix.net