Hello,
Hope you can help.
I have simplified my sketch to two buttons (of 16), each button should call a draw method of a shape rotated, (pop1c for button one) and (pop2c for button two).
Because I have a method doAll() the pop1c(20, 20) & pop2c(80, 80) need to be incorporated at the bottom of the doAll() method to run, BUT this means they are run at the same time. How can I separate them? I enclose my code, and further down the button code to go on a separate tab.
//MAIN SKETCH
int x ;
int y ;
int a =0;
int i =0;
Button button1, button2;
boolean checker, checker2;
void setup() {
size(800, 800);
background(255);
boolean checker = false;
boolean checker2 = false;
button1 = new Button (x+20, y+20, 60);
button2 = new Button (x+120, y+20, 60);
}
void draw() {
background(255);
button1.update();//display button
button1.display();//refresh every frame
button2.update();//display button
button2.display();//refresh every frame
if (checker) { //If the button is pressed run shape rotated and translated
doAll();//new method that contains translate, rotate
}
}
void mousePressed() {
if (button1.press()) {
checker = true;
checker2 = false;
print("BUT1");
}
else if (button2.press()) {
checker = true;
checker2 = false;
print("BUT2");
}
}
void doAll() {//method to rotate and translate shape
frameRate(5);
translate(400, 400);
for (int i=0; i<32; i++) {
rotate(PI/14.0);
smooth();
strokeWeight(0.5);
pop1c(20, 20);
pop2c(80, 80);
}
}
void pop1c(int x, int y) {//Circle1
int c1 = (int)random(-1, 32);
ellipse(100, 0, c1-8, c1+5);
}
void pop2c(int x, int y) {
int c2 = (int)random(-2, 8);
ellipse(80, 0, c2-10, c2);
}
//BUTTON FOR SEPARATE TAB
class Button{
int x, y;//variables for X&Y coordinates
int size;//size of button width and height
boolean over = false;
boolean pressed = false;
Button(int xp, int yp, int s){
x = xp;
y = yp;
size = s;
}
void update(){//updates the over field every time
if ((mouseX >= x) && (mouseX <= x + size) &&
(mouseY >= y) && (mouseY <= y + size)){
over = true;
} else {
over = false;
}
}
boolean press(){
if (over == true){
pressed = true;
return true;
} else {
return false;
}
}
void release(){
pressed = false;
}
void display()
{
rect(x,y,size,size);
}
}
Hope you can help.
I have simplified my sketch to two buttons (of 16), each button should call a draw method of a shape rotated, (pop1c for button one) and (pop2c for button two).
Because I have a method doAll() the pop1c(20, 20) & pop2c(80, 80) need to be incorporated at the bottom of the doAll() method to run, BUT this means they are run at the same time. How can I separate them? I enclose my code, and further down the button code to go on a separate tab.
//MAIN SKETCH
int x ;
int y ;
int a =0;
int i =0;
Button button1, button2;
boolean checker, checker2;
void setup() {
size(800, 800);
background(255);
boolean checker = false;
boolean checker2 = false;
button1 = new Button (x+20, y+20, 60);
button2 = new Button (x+120, y+20, 60);
}
void draw() {
background(255);
button1.update();//display button
button1.display();//refresh every frame
button2.update();//display button
button2.display();//refresh every frame
if (checker) { //If the button is pressed run shape rotated and translated
doAll();//new method that contains translate, rotate
}
}
void mousePressed() {
if (button1.press()) {
checker = true;
checker2 = false;
print("BUT1");
}
else if (button2.press()) {
checker = true;
checker2 = false;
print("BUT2");
}
}
void doAll() {//method to rotate and translate shape
frameRate(5);
translate(400, 400);
for (int i=0; i<32; i++) {
rotate(PI/14.0);
smooth();
strokeWeight(0.5);
pop1c(20, 20);
pop2c(80, 80);
}
}
void pop1c(int x, int y) {//Circle1
int c1 = (int)random(-1, 32);
ellipse(100, 0, c1-8, c1+5);
}
void pop2c(int x, int y) {
int c2 = (int)random(-2, 8);
ellipse(80, 0, c2-10, c2);
}
//BUTTON FOR SEPARATE TAB
class Button{
int x, y;//variables for X&Y coordinates
int size;//size of button width and height
boolean over = false;
boolean pressed = false;
Button(int xp, int yp, int s){
x = xp;
y = yp;
size = s;
}
void update(){//updates the over field every time
if ((mouseX >= x) && (mouseX <= x + size) &&
(mouseY >= y) && (mouseY <= y + size)){
over = true;
} else {
over = false;
}
}
boolean press(){
if (over == true){
pressed = true;
return true;
} else {
return false;
}
}
void release(){
pressed = false;
}
void display()
{
rect(x,y,size,size);
}
}
1