How too make two codes work together at random?!

Please help!

I have two processing codes I have made from scratch, one moves text quick and one changes it's colour both at random. I am wanting to put the two codes together so that when the sketch is run it picks at random what code is run? I can't find anything on how to do this, even bought the handbook and that's not helped,

First Code

int A;
int speed;
PFont a;  


void setup() {
  size(500,500);
  A = 150;
  speed = 5;
    a = createFont("OCRA",250,true);
frameRate(random(100));
}

void draw() {

  background(100,100,100);
  fill(0,0,0);
  textFont(a);
  float r = random(100);
  if (r > random(100)) {
  A = A + speed;

  if(A > 125) {
    speed = -speed;
  }

  if(A < r) {
      speed = -speed;
    }
       text("B",A,350);

}
}

Second code

int A;
int speed;
PFont a;  


void setup() {
  size(500,500);
  A = 150;
  speed = 5;
    a = createFont("OCRA",250,true);
frameRate(random(0,50));
}

void draw() {

  background(100,100,100);
  fill(random(0,255),random(0,255),random(0,255));
  textFont(a);
  float r = random(100);

       text("B",A,350);
    }

Please help!

Thank you In advance Plowrighth

Tagged:

Answers

  • Answer ✓

    the simplest way is to move the two separate draws into methods with different names and then call the relevant one from draw().

    int rand = (int)random(2);
    
    ...
    
    draw() {
      switch (rand) {
        case 0:
          drawFast();
          break;
        case 1:
          drawColours();
          break;
      }
    }
    

    common stuff can go outside the switch

    some random tips:

    ctrl-t in the editor will format the code nicely.

    don't have things called both a and A. call things sensible names. variables traditionally start with a lowercase letter, uppercase is reserved for class names.

  • @plowrighth --

    There are several ways of coordinating multiple sketches.

    1. One combined sketch. This is the approach recommended by @koogs -- in your case, this seems like the right way to do it. Other, more complex options:

    2. A Processing sketch launcher. First export your SketchA and SketchB as apps (File > Export Application). Then create a new Sketch, SketchLauncher. This will contain the random statement, and then launch either SketchA or SketchB using the launch() command.

    3. A launching script. Write it in another scripting language that runs on your operating system (e.g. Python, Ruby, AppleScript). Many have features for starting and stopping programs that Processing doesn't. See discussion of this and more links in the thread Launching Scripts.

  • Great, thank you both, very helpful as I'm sure you can tell I'm new to this. Thank you again

  • edited October 2016

    So when I add a third draw into the random mix? what would I change? I've got them two working and tried doing it myself, but i get an unexpected token: void, bug returned? Thanks again

  • edited October 2016

    It is easier to give feedback if you share code.

    "unexpected token: void" probably means that you forgot to close your previous function with '}' on the line number just before 'void myfunction() {'.

    The compiler got to 'void' and says -- wait, we were in the middle of talking about the last function, and never stopped. That was unexpected, to see 'void' mentioned here!

    Adding third fourth fifth draw etc. etc.:

    int rand;
    void setup(){
      rand = (int)random(5); // number of cases
    }
    void draw() {
      switch (rand) {
        case 0: drawA(); break;
        case 1: drawB(); break;
        case 2: drawC(); break;
        case 3: drawD(); break;
        case 4: drawE(); break;
      }
    }
    void drawA() {} // code here
    void drawB() {} // code here
    void drawC() {} // code here
    void drawD() {} // code here
    void drawE() {} // code here
    

    How many different draws are you doing? If dozens or more at some point you might want to consider one of the other options mentioned above.

  • Yeah, I had worked it out, and you are right it was that I had missed off a } to finish the previous draw :) thanks though!

Sign In or Register to comment.