Hi there,
I am combining 3 different (but similar) sketches into one sketch, and using switch
to change states. Using the keyboard input of ‘1’, ‘2’, ‘3’. However, my sketch does not switch
when I press the keys. Because it is easier for me to stay organized, I have included each sketch in a different tab, and only having one setup()
and one draw()
function in the main tab, and then calling the other tabs from there. I named each sketch / scene, draw1(), draw2(), draw3()
and I am calling them from the MAIN draw()
Eventually I will try and implement timed events using a timer or frameCount
, or FrequencyEnergyBeatDetection
so that each sketch shows itself depending on the frequency being played, but for the moment I am trying to switch states
using numbers on my keyboard. I also did not know if this is the correct way or if I could somehow include each individual sketch in a PGraphics
object?
I implemented a small example with much simpler code that works, using the Amnon example found here: https://amnonp5.wordpress.com/2012/01/28/25-life-saving-tips-for-processing/ he uses the void mousePressed()
function but I was able to implement void keyPressed()
in his example and it works.
It does not work when I implement it in mine.
I am using P3D, and I don’t know if that is the problem. Only pasting relevant code in the example, but there is a sound track for the animation that I will leave in just so you know, for clarity purposes, as I don’t know that that is related. Thank you for reading this. Any help would be appreciated.
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import ddf.minim.signals.*;
import ddf.minim.spi.*;
import ddf.minim.ugens.*;
Minim minim;
AudioPlayer song;
BeatDetect beat;
BeatListener bl;
float kickSize, snareSize, hatSize;
int sketchToDraw;
void setup() {
size(600, 600, P3D);
smooth();
background(0); //0xeeeeff
minim = new Minim(this);
song = minim.loadFile("Kintsugi (09_04_20).mp3", 1024);
song.play();
}
void draw() {
if(song==null)
return;
switch (sketchToDraw) {
case 0: draw1(); break;
case 1: draw2(); break;
case 2: draw3(); break;
default: background(0); break;
}
}
void keyPressed() {
if (key == '1')
sketchToDraw = 0;
if (key == '2')
sketchToDraw = 1;
if (key == '3')
sketchToDraw = 2;
}
//////////////////////// other sketches / scenes in tabs below: draw1(), draw2(), draw3()
//// that I call from draw() using switch
void draw1() {
float speed = 0.003f;//0.03f;
float radius1 = 50 + 30 * sin( frameCount * speed);
float radius2 = 50 +30 * cos(frameCount * speed);
if (mousePressed) {
background(255);
//filter(ERODE);
filter(INVERT);
strokeWeight(100 * sin(frameCount * speed));
stroke(#81FFE0, 255); //255
fill(0, 0);
rotateX(radians(radius1));
} else {
strokeWeight(sin(frameCount * speed));
stroke(#81FFE0, random(100, 200));
fill(255, radius1, radius2, 3);
}
pushMatrix();
//frameRate(30);
//background(0);
lights();
translate(width/2, height/2, 200);
rotateZ(radians(radius2));
rotateX(PI * sin(radians(radius1*500)));
rotateY(radians(radius2 * speed));
//println(sp);
sphere(constrain(radius2*3, 20, 1000)); //1000
//ellipse(mouseX, mouseY, radius1 * 15, radius2 * 15);
popMatrix();
pushMatrix();
lights();
translate(width/2, height/2, 200);
rotateY(radius1 * 500);
rotateZ(radius2 * 1000);
sphere(800);
popMatrix();
}
void draw2() {
float speed = 0.003f;//0.03f;
float radius1 = 50 + 30 * sin( frameCount * speed);
float radius2 = 50 +30 * cos(frameCount * speed);
if (mousePressed) {
background(255);
filter(ERODE);
//filter(INVERT);
strokeWeight(10 * sin(frameCount * speed));
stroke(#81FFE0, 150); //255
fill(0, 255);
rotateX(radians(radius1));
} else {
strokeWeight(sin(frameCount * speed));
stroke(#81FFE0, random(100, 200));
fill(255, radius1, radius2, 3);
}
pushMatrix();
// frameRate(30);
//background(0);
lights();
translate(width/2, height/2, 200);
rotateZ(radians(radius2));
rotateX(PI * sin(radians(radius1*500)));
rotateY(radians(radius2 * speed));
//println(sp);
sphere(constrain(radius2*3, 20, 1000)); //1000
pushStyle();
filter(POSTERIZE,230); // FILTER!!!
popStyle();
//ellipse(mouseX, mouseY, radius1 * 15, radius2 * 15);
popMatrix();
pushMatrix();
lights();
translate(width/2, height/2, 200);
rotateY(radius1 * 500);
rotateZ(radius2 * 1000);
sphere(800);
popMatrix();
}
void draw3() {
float speed = 0.003f;//0.03f;
float radius1 = 50 + 30 * sin( frameCount * speed);
float radius2 = 50 +30 * cos(frameCount * speed);
if (mousePressed) {
background(255);
filter(ERODE);
//filter(INVERT);
strokeWeight(100 * sin(frameCount * speed));
stroke(#81FFE0, 255); //255
fill(0, 0);
rotateX(radians(radius1));
} else {
strokeWeight(sin(frameCount * speed));
stroke(#81FFE0, random(100, 200));
fill(255, radius1, radius2, 3);
}
pushMatrix();
//frameRate(30);
//background(0);
lights();
translate(width/2, height/2, 200);
rotateZ(radians(radius2));
rotateX(PI * sin(radians(radius1*500)));
rotateY(radians(radius2 * speed));
//println(sp);
sphere(constrain(radius2*3, 20, 1000)); //1000
//ellipse(mouseX, mouseY, radius1 * 15, radius2 * 15);
popMatrix();
pushMatrix();
lights();
translate(width/2, height/2, 200);
rotateY(radius1 * 500);
rotateZ(radius2 * 1000);
sphere(800);
popMatrix();
}