Activar botón y volver a escena intermedia

Change case “millis5000” to this :

case "millis5000":
   scene2();
   if (mousePressed && dist(mouseX, mouseY, width/2, height/1.35) < height/9) {
      offset = second();
      scene = "scene3";
   }
   break;

It‘s basically just adding offset = second(); in the if statement, but you‘ll also have to add the brackets, since if you have multiple lines you can’t leave them out (only if it‘s a Single line in the if statement).

Thanks a lot @Lexyth, now it’s perfect! I tried to come back to scene 2 adding mousePressed(), but I didn’t achieve it. Can you tell me if it’s right?

void draw () {
   switch (scene) {
      case "initial":
         if (millis() > 5000) scene = "millis5000";
         scene1();
         break;
     case "millis5000":
         scene2();
         if (mousePressed && dist(mouseX, mouseY, width/2, height/1.35) < height/9) {
          offset = second();
          scene = "scene3";
   }
         break;
      case "scene3" :
         scene3();
         break;
      default:
         scene1();
         break;
      // I agregated this to come back to scene 2 while you see the clock   
      case "scene2":
         if (mousePressed) {
         scene = "scene2";  
         }
         break;
          
   } 
}

In another vein, just out of curiosity, how could I change clock hand’s speed from 1 second to 1,5 s. It would be so convoluted?

Thanks!

1 Like

The clock Hand speed is relative to the second() method, which returns the actual time. You‘ll just have to change every second() in your Code to (second() * 1.5).

As for what you tried to achieve, no, the case “scene2” will never be called this way… but i‘m not sure what exactly you want to achieve, so please make that a bit clearer.

Edit : If you want to be at scene3 (clock) while pressing the mouse and get back to scene2 (instructions) when you release the mouse, then i already posted how to do that.

Else, if you want to toogle (first click opens clock, then clicking again later closes clock and opens instructions), then you‘ll have to do it a bit different. You‘ll want to add if mousePressed) scene = “The other scene”;. So in millis5000 is would be scene3 and in scene3 it would be millis5000.

1 Like

Thanks @Lexyth,

Yes, I want to come back to scene 2 frome scene 3 by touching the screen. I added mouseReleased at the end of the code, but it doesn’t work.

void draw () {
   switch (scene) {
      case "initial":
         if (millis() > 5000) scene = "millis5000";
         scene1();
         break;
     case "millis5000":
         scene2();
         if (mousePressed && dist(mouseX, mouseY, width/2, height/1.3) < height/9) {
          offset = second();
          scene = "scene3";
   }
         break;
      case "scene3" :
         scene3();
         break;
      default:
         scene1();
         break;
         if (mouseReleased) {
         scene = "scene2";
   }
   } 
}

Is it correct?

I also tried this, like on processing reference, but It doesnt work.

void draw () {
   switch (scene) {
      case "initial":
         if (millis() > 5000) scene = "millis5000";
         scene1();
         break;
     case "millis5000":
         scene2();
         if (mousePressed && dist(mouseX, mouseY, width/2, height/1.3) < height/9) {
          offset = second();
          scene = "scene3";
   }
         break;
      case "scene3" :
         scene3();
         break;
      default:
         scene1();
         break;
         
      void mouseReleased(){
           if (mousePressed) {
           scene = "scene2";
           }
   } 
}

You can‘t declare another method inside a method like that…

This is how you‘d go back to scene2 from scene3 with a mousePress.

boolean pressIsNew = false;

void draw () {
   switch (scene) {
      case "initial":
         if (millis() > 5000) scene = "millis5000";
         scene1();
         break;
     case "millis5000":
         scene2();
         if (mousePressed && dist(mouseX, mouseY, width/2, height/1.3) < height/9) {
            offset = second();

pressIsNew = false;

            scene = "scene3";
         }
         break;
      case "scene3" :

if (!mousePressed) pressIsNew = true;
if (mousePressed && pressIsNew) scene = "millis5000";

         scene3();
         break;
      default:
         scene1();
         break;
   } 
}

Add these three lines (the spaced out lines) and it should work.

Thank you so much, @Lexyth, but with that code you always see scene 2 while you are touching the screen, what I need is coming back permanentely to scene 2 from scene 3. Then, you can go again to scene 3 by touching the button, but the point it is to come back to scene 2 and be able to stay there. I tryed with mouseReleased as you tell me, but I didn’t get it. Can you help me, please?

1 Like

Blockquote The mousePressed() function is called once after every time a mouse button is pressed.

In my example I set states and made a decision based on those; I found this much easier to keep track of.

void draw () 
  {
  switch (scene) 
    {
    case "initial":
      if (millis() > 2000) scene = "Instrucciones";
      scene1();
      sceneState = 1;
      break;
    
    case "Instrucciones":
      scene2();
      sceneState = 2;
      break;
    
    case "scene3" :
      scene3();
      sceneState = 3;
      break;
    
    default:
      scene1();
      break; 
    } 
  }  
  
void mousePressed()
  {
  if (sceneState == 2) 
    {
    if (dist(mouseX, mouseY, width/2, height/1.35) < height/9)
      {
      offset = second();
      scene = "scene3";
      }
    else
      println("Press the Button");
    }          
  
  if (sceneState == 3) 
    {
    scene = "Instrucciones";
    }
  
  println("Last sceneState: " + sceneState);    
  }

:slight_smile:

thanks a lot, @glv, but i think there has been a misunderstanding. What I need is getting out of scene3 and returning and staying in scene2 when you touch the screen, but it has to been definitive. Then, the way to go again to scene3 is pressing the button.

Yes indeed, i thought the pressIsNew would be enough to also make it work in scene 2 (which it does, unless your mouse is exactly where the Button was…). I fixed it to account for the Button.

void draw () {
   switch (scene) {
      case "initial":
      if (millis() > 5000) scene = "millis5000";
      scene1();
      break;
      case "millis5000":
      scene2();
      if (pressIsNew && mousePressed && dist(mouseX, mouseY, width/2, height/1.3) < height/9) {
         offset = second();
         
         pressIsNew = false;
         
         scene = "scene3";
      }
      if (!mousePressed) pressIsNew = true;
      break;
      case "scene3" :
      
      
      if (mousePressed && pressIsNew) {
         scene = "millis5000";
         pressIsNew = false;
      }
      
      if (!mousePressed) pressIsNew = true;
      
      scene3();
      break;
      default:
      scene1();
      break;
   } 
}

My example was tested and worked.
I did not “correct” your code but offered a minimal code alternative for consideration that kept track of the state of buttons.

In scene 3 if I clicked mouse anywhere it would go back to scene 2.
In scene 2 if I clicked the button it would go to scene 3.

I do not have a touchscreen so unable to test this and used the mouse.

A modified and minimal code version with the scenes simplified and button removed:

int sceneState;
String scene = "scene_01";

void setup() 
  {
  size(500, 500);  
  noStroke();
  textAlign(CENTER, CENTER);
  textSize(48);
  } 

void draw () 
  {
  background(0);
  
  //Removed counter
    
  switch (scene) 
    {
    case "scene_01":
      if (millis() > 2000) scene = "scene_02";
      scene01(); 
      sceneState = 1;
      break;
    
    case "scene_02":
      scene02();
      sceneState = 2;
      break;
    
    case "scene_03" :
      scene03();
      sceneState = 3;
      break;
    
    default:
      scene01();
      break; 
    } 
  }  
  
void mousePressed()
  {
  if (sceneState == 2) 
    {
    // Removed code to check of overCircle() and update scene
    scene = "scene_03";
    //count = 0;
    }          
  
  if (sceneState == 3) 
    {
    scene = "scene_02";
    }
  
  println("Last sceneState: " + sceneState);    
  }
  
// Removed function overCircle() that checked if "button pressed"

  void scene01() 
  {
  background(255, 0, 0);
  text("SCENE 1", width/2, height/3);
  }

void scene02() 
  {
  background(0, 255, 0);
  text("SCENE 2", width/2, height/3);
  // Removed circle
  }

void scene03() 
  {
  background(0, 0, 255);
  text("SCENE 3", width/2, height/3);
  //Removed display showing time elapsed
  }

Have fun coding!

I enjoyed this topic and can use what I have learned in the future.

:slight_smile:

1 Like

oh, @Lexyth, many thanks for your help, the app is really finished!

Where do I have to add the SDK code from Google AdMob to play and advertisment when an user press button?

ca-app-pub-8098254753595764~4477725855

1 Like

Thank you glv, it is really useful por apps programming.

I have no idea about that, but as far as i know, that ad stuff is a lot more complicated to implement than making the App itself… already tried that a couple of times (and always failed…), though in eclipse, not Processing, so idk. You might want to just Google it and see if there‘s an answer already.

thans @Lexyth, I’ll tried with eclipse. :blush:

hi @Lexyth

when I try to export a signed package of my sketch on android mode, there are 9 errors on the console:

  1. ERROR in C:\Users\nieto\AppData\Local\Temp\android6393256656718286809sketch\app\src\main\java\com\deepfocus\android\sketch_191212a.java (at line 19)
    nt cx, cy;
    ^^
    nt cannot be resolved to a type

  1. ERROR in C:\Users\nieto\AppData\Local\Temp\android6393256656718286809sketch\app\src\main\java\com\deepfocus\android\sketch_191212a.java (at line 36)
    cx = width / 2;
    ^^
    nt cannot be resolved to a type

  1. ERROR in C:\Users\nieto\AppData\Local\Temp\android6393256656718286809sketch\app\src\main\java\com\deepfocus\android\sketch_191212a.java (at line 37)
    cy = height / 2;
    ^^
    nt cannot be resolved to a type

  1. ERROR in C:\Users\nieto\AppData\Local\Temp\android6393256656718286809sketch\app\src\main\java\com\deepfocus\android\sketch_191212a.java (at line 108)
    line(cx, cy, cx + cos(s) * secondsRadius, cy + sin(s) * secondsRadius);
    ^^
    nt cannot be resolved to a type

  1. ERROR in C:\Users\nieto\AppData\Local\Temp\android6393256656718286809sketch\app\src\main\java\com\deepfocus\android\sketch_191212a.java (at line 108)
    line(cx, cy, cx + cos(s) * secondsRadius, cy + sin(s) * secondsRadius);
    ^^
    nt cannot be resolved to a type

  1. ERROR in C:\Users\nieto\AppData\Local\Temp\android6393256656718286809sketch\app\src\main\java\com\deepfocus\android\sketch_191212a.java (at line 108)
    line(cx, cy, cx + cos(s) * secondsRadius, cy + sin(s) * secondsRadius);
    ^^
    nt cannot be resolved to a type

  1. ERROR in C:\Users\nieto\AppData\Local\Temp\android6393256656718286809sketch\app\src\main\java\com\deepfocus\android\sketch_191212a.java (at line 108)
    line(cx, cy, cx + cos(s) * secondsRadius, cy + sin(s) * secondsRadius);
    ^^
    nt cannot be resolved to a type

  1. ERROR in C:\Users\nieto\AppData\Local\Temp\android6393256656718286809sketch\app\src\main\java\com\deepfocus\android\sketch_191212a.java (at line 115)
    float x = cx + cos(angle) * secondsRadius;
    ^^
    nt cannot be resolved to a type

  1. ERROR in C:\Users\nieto\AppData\Local\Temp\android6393256656718286809sketch\app\src\main\java\com\deepfocus\android\sketch_191212a.java (at line 116)
    float y = cy + sin(angle) * secondsRadius;
    ^^
    nt cannot be resolved to a type

9 problems (9 errors)

SOS! Can you help me? This is last version of my code:

int cx, cy;
int offset;

float secondsRadius;
float clockDiameter;



void setup(){
  size(270,540);
  background(0,0,0);
  noStroke();
  offset = second();
  int radius = min(width, height) / 2;
  secondsRadius = radius * 0.72;
  clockDiameter = radius * 1.8;
 
  cx = width / 2;
  cy = height / 2;
 
}

//first scene lasts 5 seconds
void scene1() {
  background(0,0,0);
  fill(255, 255, 255, 255);
  textSize(height/12);
  textAlign(RIGHT, CENTER);  
  text("deep", width/2, height/2);
  fill(255, 204 , 0, 255);
  textSize(height/12);
  textAlign(LEFT, CENTER);
  text("focus", width/2, height/2);
  fill(255, 95, 95, 255);
  textSize(height/36);
  textAlign(CENTER, CENTER);
  text("@deepfocusapp", width/2, height/1.1);
 
}

//second scene has a button that activates third scene
void scene2() {
  String a = "1. Cuenta del 100 al 1 siguiendo la aguja del reloj. Cuando llegues al 1, vuelve a empezar.";
  String b = "2. Cada vez que te distraigas, devuelve tu atención a la manecilla y continúa con la cuenta descendente.";
  String c = "Instrucciones.";
 
  background(255);
  fill(0);
  stroke(255,95,95);
  strokeWeight(1);
  line(width/10, height/5, width/1.15, height/5);
  textAlign(LEFT, TOP);
  textSize(height/22);
  text(c, width/10, height/10, width/1.2, height/6.66);
  textSize(height/34);
  text(a, width/10, height/4, width/1.2, height/2.85);
  textSize(height/34);
  text(b, width/10, height/2.22, width/1.2, height/2);
 
  //this is my button
  stroke(255,95,95);
  strokeWeight(55);
  point(width/2, height/1.3);
  stroke(255);
  strokeWeight(30);
  point(width/2, height/1.3);
  stroke(255,95,95);
  strokeWeight(25);
  point(width/2, height/1.3);
  stroke(255);
  strokeWeight(5);
  point(width/2, height/1.3);
 
}
   
void scene3(){
  background(247,108,127);
  float l = width/255;
  strokeWeight(1);
  for(int i = 0; i<width; i++) {
  stroke(252,150,120,255-i/(l));
  line(i,0,i, height);
}
 

  float s = -map(second()-offset, 0, 10, 0, TWO_PI) -HALF_PI;

  stroke(255);
  strokeWeight(5);
  line(cx, cy, cx + cos(s) * secondsRadius, cy + sin(s) * secondsRadius);


   strokeWeight(5);
  beginShape(POINTS);
  for (int a = 0; a < 360; a+=36) {
    float angle = radians(a)+HALF_PI;
    float x = cx + cos(angle) * secondsRadius;
    float y = cy + sin(angle) * secondsRadius;
    vertex(x, y);
  }
  endShape();
}
 
String scene = "initial";
boolean pressIsNew = false;

void draw () {
   switch (scene) {
      case "initial":
      if (millis() > 5000) scene = "millis5000";
      scene1();
      break;
      case "millis5000":
      scene2();
      if (pressIsNew && mousePressed && dist(mouseX, mouseY, width/2, height/1.3) < height/9) {
         offset = second();
         
         pressIsNew = false;
         
         scene = "scene3";
      }
      if (!mousePressed) pressIsNew = true;
      break;
      case "scene3" :
     
     
      if (mousePressed && pressIsNew) {
         scene = "millis5000";
         pressIsNew = false;
      }
     
      if (!mousePressed) pressIsNew = true;
     
      scene3();
      break;
      default:
      scene1();
      break;
   }
}

oh my god, i found the error, i wrote a mistake.

nt cx, cy;
nt cannot be resolved to a type

Was it a missing “i” in “int” ?

1 Like

Hi, @Lexyth

My app is finished but I need to change hand clock speed from 1 second to 2 seconds. I have changed every second() in my code to (second()*2), but the clock breaks. How could I make that the hand clock moves every 2 second, but without jumping any of the 10 points? Please remember the hand clock has to start always at 12 o clock.

Thank you!!!

int cx, cy;

float offset;
float secondsRadius;
float clockDiameter;



void setup(){
  size(270,540);
  background(0,0,0);
  noStroke();
  offset = (second()*2);
  int radius = min(width, height) / 2;
  secondsRadius = radius * 0.72;
  clockDiameter = radius * 1.8;
 
  cx = width / 2;
  cy = height / 2;
 
}

//first scene lasts 5 seconds
void scene1() {
  background(0,0,0);
  fill(255, 255, 255, 255);
  textSize(height/12);
  textAlign(RIGHT, CENTER);  
  text("deep", width/2, height/2);
  fill(255, 204 , 0, 255);
  textSize(height/12);
  textAlign(LEFT, CENTER);
  text("focus", width/2, height/2);
  fill(255, 95, 95, 255);
  textSize(height/36);
  textAlign(CENTER, CENTER);
  text("@deepfocusapp", width/2, height/1.1);
 
}

//second scene has a button that activates third scene
void scene2() {
  String a = "1. Cuenta del 100 al 1 siguiendo la aguja del reloj. Cuando llegues al 1, vuelve a empezar.";
  String b = "2. Cada vez que te distraigas, devuelve tu atención a la manecilla y continúa con la cuenta descendente.";
  String c = "Instrucciones.";
 
  background(255);
  fill(0);
  stroke(255,95,95);
  strokeWeight(1);
  line(width/10, height/5, width/1.15, height/5);
  textAlign(LEFT, TOP);
  textSize(height/22);
  text(c, width/10, height/10, width/1.2, height/6.66);
  textSize(height/34);
  text(a, width/10, height/4, width/1.2, height/2.85);
  textSize(height/34);
  text(b, width/10, height/2.22, width/1.2, height/2);
 
  //this is my button
  stroke(255,95,95);
  strokeWeight(55);
  point(width/2, height/1.3);
  stroke(255);
  strokeWeight(30);
  point(width/2, height/1.3);
  stroke(255,95,95);
  strokeWeight(25);
  point(width/2, height/1.3);
  stroke(255);
  strokeWeight(5);
  point(width/2, height/1.3);
 
}
   
void scene3(){
  background(247,108,127);
  float l = width/255;
  strokeWeight(1);
  for(int i = 0; i<width; i++) {
  stroke(252,150,120,255-i/(l));
  line(i,0,i, height);
}
 

  float s = -map((second()*2)-offset, 0, 10, 0, TWO_PI) -HALF_PI;

  stroke(255);
  strokeWeight(5);
  line(cx, cy, cx + cos(s) * secondsRadius, cy + sin(s) * secondsRadius);


   strokeWeight(5);
  beginShape(POINTS);
  for (int a = 0; a < 360; a+=36) {
    float angle = radians(a)+HALF_PI;
    float x = cx + cos(angle) * secondsRadius;
    float y = cy + sin(angle) * secondsRadius;
    vertex(x, y);
  }
  endShape();
}
 
String scene = "initial";
boolean pressIsNew = false;

void draw () {
   switch (scene) {
      case "initial":
      if (millis() > 5000) scene = "millis5000";
      scene1();
      break;
      case "millis5000":
      scene2();
      if (pressIsNew && mousePressed && dist(mouseX, mouseY, width/2, height/1.3) < height/9) {
         offset = second();
         
         pressIsNew = false;
         
         scene = "scene3";
      }
      if (!mousePressed) pressIsNew = true;
      break;
      case "scene3" :
     
     
      if (mousePressed && pressIsNew) {
         scene = "millis5000";
         pressIsNew = false;
      }
     
      if (!mousePressed) pressIsNew = true;
     
      scene3();
      break;
      default:
      scene1();
      break;
   }
}
1 Like

The only place where you need to change something is this one, the rest should go back to how it was before :

This is for slowing down only!

float s = -map(floor(((second())-offset)/2), 0, 10, 0, TWO_PI) -HALF_PI;

//or you can also do this : 

final float SPEED = 2; //this is a global variable!
//you can change this one how you like in Code, but you can‘t change it!
//it‘s a constant (you can‘t change them at runtime)

float s = -map(floor(((second())-offset)/SPEED), 0, 10, 0, TWO_PI) -HALF_PI;

To Speed up you can‘t use second(), because it‘s only changed once a second! Theres no way to know if it‘s half way through or just startelf out. You‘ll have to change your Code to use millis() if you want that. You could just use floor(millis()/1000) and then for the speedup, floor(millis()/(1000/SPEED)).

float s = -map(floor((millis()-offset)/(1000*SPEED)), 0, 10, 0, TWO_PI) -HALF_PI;

Just replace this line and change all second() to millis().

1 Like