# Only countdown when mouse at a certain position

**URL:** <https://discourse.processing.org/t/only-countdown-when-mouse-at-a-certain-position/15635>\
**Category:** Coding Questions\
**Created:** [November 19, 2019, 12:16pm UTC](https://discourse.processing.org/t/only-countdown-when-mouse-at-a-certain-position/15635 "2019-11-19T12:16:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kinglucas](https://avatars.discourse-cdn.com/v4/letter/k/53a042/32.png) [@kinglucas](https://discourse.processing.org/u/kinglucas)\
**Post date:** [November 19, 2019, 12:16pm UTC](https://discourse.processing.org/t/only-countdown-when-mouse-at-a-certain-position/15635/1 "2019-11-19T12:16:20Z")

</div>

I want a countdown timer only when the mouse is on the bottom left rectangle the timer counts down and if you are not in that part it doesnt count down. Thank you in advance.

```auto
// IMPORTANT!!! READ BELOW
// Every scene is on a different page so it's organized
// The scenes are voids so you just have to call them in void draw
//-----------------------------------------------------------------------------

int scene = 3;
int messageCounter=0;     
boolean message = true;

boolean canShoot = false;

PImage Sheriff;
PImage Box;
PImage Logo;
PImage Story1;
PImage Story2;
PImage Bandit;
PFont dialogueText;

float transparency = 255;

//textposition
int textX = 500;
int textY = 900;

//boxposition
int boxX = 1920;
int boxY = 1000;

//timer
int begin;
int duration = 10;
int time = 10;

//-----------------------------------------------------------------------------

void setup() {
  fullScreen();
  Sheriff = loadImage("Sheriff.png");
  Box = loadImage("Dialogue box.png");
  Logo = loadImage("Logo.png");
  Story1 = loadImage("story1.jpg");
  Story2 = loadImage("story2.png");
  Bandit = loadImage("bandit.png");

  dialogueText = createFont("Carnevalee Freakshow.ttf", 72);

  begin = millis();
}

//-----------------------------------------------------------------------------

void draw() {
  textFont(dialogueText);
  if (scene == 0) {
    Scene0();
  } else if (scene == 1) {
    Scene1();
  } else if (scene == 2) {
    Scene2();
  } else if (scene == 3) {
    shootingScene();
  } else if (scene == 4) {
    creditScene();
  }
}

//-----------------------------------------------------------------------------

void mousePressed() {
  messageCounter++;
  if (message == false && mousePressed & 
    isPointInRectangle(mouseX, mouseY, width-50, 300, 40, 40)) {
    scene = scene + 1; // if you click on the triangle on the screen you go to the next scene
    message = true; // reset
    messageCounter=0;
  }
}

//-----------------------------------------------------------------------------

boolean isPointInRectangle(float px, float py, 
  float rx, float ry, 
  float rw, float rh) {
  if (px >= rx &&
    px <= rx + rw &&
    py >= ry &&
    py <= ry + rh) {
    return true;
  }
  return false;
}

//-----------------------------------------------------------------------------

```

```auto
void Scene1() {
  background(255, 0, 0);

  image(Sheriff, 0, 250, 400, 800);

  //-----------------------------------------------------------------------------

  if (message == true) {
    //drawing the text box
    image(Box, 0, 650, boxX, boxY);
    //end of text box
  }

  //-----------------------------------------------------------------------------

  //drawing the arrow only when the text is finished
  if (message == false) {
    fill(0);
    triangle(width-50, 300, width-50, 340, width-10, 320);

    if (message == false & isPointInRectangle(mouseX, mouseY, width-50, 300, 40, 40)) {
      strokeWeight(3);
      stroke(255);
      triangle(width-50, 300, width-50, 340, width-10, 320); //Copy and paste this for the arrows to light up when mouse if hovering
    } else if (message == false) {
      noStroke();
      triangle(width-50, 300, width-50, 340, width-10, 320);
    }
  }
  //end of the arrow

  //-----------------------------------------------------------------------------
  if (messageCounter==0) {
    image(Story1, 0, 0);
  }
  if (messageCounter==1) {
    image(Story2, 0, 0);
  }
  if (messageCounter==2) {          
    textSize(35); //test dialogue for now. If you press the mouse there is dialogue
    fill(0);
    text("You: Howdy partner", textX, textY);
  } else if (messageCounter==3) {          
    textSize(35); //test dialogue for now. If you press the mouse there is dialogue
    fill(0);
    text("Clerk: goodday what is it you need", textX, textY); //This is where you add text for the dialogue
  } else if (messageCounter==4) {          
    textSize(35); //test dialogue for now. If you press the mouse there is dialogue  
    fill(0);
    text(" No ", textX, textY);
  } else if (messageCounter==5) {
    textSize(35); //test dialogue for now. If you press the mouse there is dialogue
    fill(0);
    text(" Maybe ", textX, textY);
  } else if (messageCounter==6) {          
    message=false;
  }

  //-----------------------------------------------------------------------------

  if (message == true) {
    fill(0);
    textSize(19);
    text("Click on the screen", width-200, height-20);
  } else {
    fill(0);
    textSize(19);
    text("Click on the Arrow", width-200, height-20);
  }
}

//-----------------------------------------------------------------------------

```

```auto
void Scene2() {
  background(255, 220, 0);

  image(Sheriff, 0, 250, 400, 800);

  //-----------------------------------------------------------------------------

  if (message == true) {
    //drawing the text box
    image(Box, 0, 650, boxX, boxY);
    //end of text box
  }

  //-----------------------------------------------------------------------------

  //drawing the arrow only when the text is finished
  if (message == false) {
    fill(0);
    triangle(width-50, 300, width-50, 340, width-10, 320);

    if (message == false & isPointInRectangle(mouseX, mouseY, width-50, 300, 40, 40)) {
      strokeWeight(3);
      stroke(255);
      triangle(width-50, 300, width-50, 340, width-10, 320); //Copy and paste this for the arrows to light up when mouse if hovering
    } else if (message == false) {
      noStroke();
      triangle(width-50, 300, width-50, 340, width-10, 320);
    }
  }
  //end of the arrow
  //-----------------------------------------------------------------------------

  if (messageCounter==0) {          
    textSize(35); //test dialogue for now. If you press the mouse there is dialogue
    fill(0);
    text("!!!This is test scene2 !!!", textX, textY);
  } else if (messageCounter==1) {
    textSize(35); //This is where you add dialogue
    fill(0);
    text("!!!This is more text !!!", textX, textY);
  } else if (messageCounter==2) {
    message = false;
  }

  //-----------------------------------------------------------------------------

  if (message == true) {    
    fill(0);
    textSize(19);
    text("Click on the screen", width-200, height-20);
  } else {
    fill(0);
    textSize(19);
    text("Click on the Arrow", width-200, height-20);
  }
}

//-----------------------------------------------------------------------------

```

```auto
void shootingScene() {

  background(0);

  //-----------------------------------------------------------------------------
  //Timer
  if (isPointInRectangle(mouseX, mouseY, 100, 1000, 50, 50)) {
    time = duration - (millis() - begin)/1000;
    canShoot = false;
  } 
  if (time <= 0) {
    time = 0;
    canShoot = true;
  }

  if (canShoot == true) {
    if(mousePressed & isPointInRectangle(mouseX, mouseY, 1250, 550, 200, 300)) {
     scene = 0; 
    }
    fill(255);
    rect(1250, 550, 200, 300);
  }

  textSize(30);
  fill(255);
  text("Time left: ", 10, 70);
  text(time, 160, 70);

  fill(255);
  rect(100, 1000, 50, 50);
  //End of timer
  
  //-----------------------------------------------------------------------------

```

```auto
//Main Menu

void Scene0() {
  background(210, 105, 30);
  //-----------------------------------------------------------------------------

  //logo
  image(Logo, 612.5, 10, 695, 400);
  //end logo

  //-----------------------------------------------------------------------------

  //menu clickable boxes
  textSize(120);                
  fill(0);
  image(Box, 0, 275, boxX, boxY);
  text("Start adventure", textX+150, 550);
  image(Box, 0, 500, boxX, boxY);
  text("Options", textX+300, 775);
  image(Box, 0, 725, boxX, boxY);
  text("Credits", textX+300, 1000);
  // end menu

  //-----------------------------------------------------------------------------

  if (mousePressed) {
    if (isPointInRectangle(mouseX, mouseY, 100, 450, 1720, 170)) {
      scene = scene + 1; //When you click on play game the game starts
      messageCounter = 0;
      message = true;
    }
  }
  
  if (mousePressed) {
   if(isPointInRectangle(mouseX, mouseY, 100, 900, 1720, 170)) {
    scene = 4; 
   }
  }

  //-----------------------------------------------------------------------------

}

```

```auto
void creditScene() {
  background(0);

  //-----------------------------------------------------------------------------

  fill(255);
  textSize(19);
  text("Thanks for playing", width/2, height/2); //credit text

  //-----------------------------------------------------------------------------

  triangle(60, 900, 60, 940, 20, 920); //arrow to go back

  //-----------------------------------------------------------------------------

  if (mousePressed && isPointInRectangle(mouseX, mouseY, 20, 900, 40, 40)) {
    scene = 0; //go back to main menu
  }
}

//-----------------------------------------------------------------------------

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 19, 2019, 2:21pm UTC](https://discourse.processing.org/t/only-countdown-when-mouse-at-a-certain-position/15635/2 "2019-11-19T14:21:08Z")

</div>

some big game framework you got already,  
and good posting format,  
BUT  
-a- without the files and fonts can not test,  
-b- and getting a full screen of a failed game  
and can not see console msg.  
is very annoying

— as soon i got it finally running i erased it.

so actually it would be better to solve your problem ( by posting ) a extra little timer project  
or called [MVCE](https://stackoverflow.com/help/minimal-reproducible-example)

even you need to invest some time, it will pay off for you and us.  
try this as a start:

```auto
// https://discourse.processing.org/t/only-countdown-when-mouse-at-a-certain-position/15635
// __________________________________________________ question about mouse over button ( ? but not pressed ? )
// __________________________________________________ and timer count down to 0 and shoot?

int b1x = 100, b1y =100, b1w =100, b1h =50;
long ts, tr=0, dt=10000; // _________________________ set 10 sec
boolean tenable = false,shoot = false;

void timer(boolean enable) {
  if ( enable ) { 
    tr = millis() - ts -dt ;
    if ( millis() - ts > dt ) {
      //println("timeout");
      shoot = true;
    }
  }
}

void button_b1() {
  timer(tenable);
  strokeWeight(2);
  if ( over(b1x, b1y, b1w, b1h) ) strokeWeight(5);
  fill(0, 0, 200);
  if ( shoot ) fill(200,0,0); 
  stroke(200, 0, 200);
  rect(b1x, b1y, b1w, b1h);

  fill(200);
  textSize(15);
  if ( over(b1x, b1y, b1w, b1h) ) {
    tenable = true;
    text( "t "+(tr/1000), b1x+5, b1y+b1h/2); // _____ show remaining time
  } else {
    tenable = false; 
    shoot = false;
    ts = millis();
    //println("reset");
  }
}

boolean over(int x, int y, int w, int h) {
  return (mouseX > x & mouseX < x + w & mouseY > y & mouseY < y + h);
}

void setup() {
  size(300, 300);
}

void draw() {
  background(200, 200, 0);
  button_b1();
}

```

also could help you about some of your coding habits.
