# How do i make doubleClicked() run without call existing mousePressed()? (p5.js)

**URL:** https://discourse.processing.org/t/how-do-i-make-doubleclicked-run-without-call-existing-mousepressed-p5-js/19982
**Category:** Coding Questions
**Created:** [April 21, 2020, 2:40am UTC](https://discourse.processing.org/t/how-do-i-make-doubleclicked-run-without-call-existing-mousepressed-p5-js/19982 "2020-04-21T02:40:25Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Royjan](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@Royjan](https://discourse.processing.org/u/Royjan)
#### Post date: [April 21, 2020, 2:40am UTC](https://discourse.processing.org/t/how-do-i-make-doubleclicked-run-without-call-existing-mousepressed-p5-js/19982/1 "2020-04-21T02:40:25Z")

</div>

Hello everybody,  
So i have a problem to call doubleClicked() function without calling existing mousePressed() function.  
Can please anyone give me a trick for that one?  
Thanks!!

---

<div class="post-metadata">

### Author: ![tony](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tony/32/949_2.png) [@tony](https://discourse.processing.org/u/tony)
#### Post date: [April 21, 2020, 3:09am UTC](https://discourse.processing.org/t/how-do-i-make-doubleclicked-run-without-call-existing-mousepressed-p5-js/19982/2 "2020-04-21T03:09:07Z")

</div>

Welcome to the forums!

Can you be more specific about the behavior you’re looking for?

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [April 21, 2020, 6:01am UTC](https://discourse.processing.org/t/how-do-i-make-doubleclicked-run-without-call-existing-mousepressed-p5-js/19982/3 "2020-04-21T06:01:52Z")

</div>

Show your entire code please and the assignment

---

<div class="post-metadata">

### Author: ![Royjan](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@Royjan](https://discourse.processing.org/u/Royjan)
#### Post date: [April 21, 2020, 7:34am UTC](https://discourse.processing.org/t/how-do-i-make-doubleclicked-run-without-call-existing-mousepressed-p5-js/19982/4 "2020-04-21T07:34:38Z")

</div>

sure, and thanks for the fast response!  
here is the exemple ( code below ):  
[https://editor.p5js.org/royjan/present/-wYWjmMaO](https://editor.p5js.org/royjan/present/-wYWjmMaO)

So i wanted to create a rectangle that switch colors randomly when i press it, it needs to start blinking when it double clicked and back to changing colors when solo pressed again.

you can see that when you double click on it it first changes its color (because of the mousePressed()) and just after that starts to blink.  
i want it to start blinking when double clicked without switching color first.

```auto
let double = false;
let time;
let count = 0;

function setup() {
	createCanvas(400, 400);
	rectangle = new Rectangle(width/2 , height/2);

}

function draw() {
	background(255);
	rectangle.show();
	rectangle.mouseOn();
	rectangle.flicker(double);
}

function doubleClicked() {
	double = true;
}

function mousePressed() {
	rectangle.pressed();
	double = false;
}

function checkcount() {

}

function Rectangle(x, y) {
	this.x = x;
	this.y = y;
	this.r = random(0, 255);
	this.g = random(0, 255);
	this.b = random(0, 255);
	this.mouseon = false;
	this.blink = 1;
	this.gamma = 255;

	this.show = function() {
		fill(this.r, this.g, this.b, this.gamma);
		rect(this.x, this.y, 150, 200);
	}

	this.mouseOn = function() {
		if (mouseX > width / 2 && mouseX < width / 2 + 150
			&& mouseY > height / 2 && mouseY < height / 2 +200) {
			this.mouseon = true;
			fill(50,50);
		    rect(this.x, this.y, 150, 200);
		} else {
			this.mouseon = false;
		}
	}

	this.pressed = function() {
		if (this.mouseon ===true) {
		console.log('mouse pressed!')
		this.r = random(0, 255);
	    this.g = random(0, 255);
	    this.b = random(0, 255);
	    var double = false;
	}
	}

	this.flicker = function(double) {
		if(double === true){
			this.blink = this.blink + 1;
			if (this.blink % 10 === 0){ 
				this.gamma = 0;
		} else {
			    this.gamma = 255;
		}
		}	
	
	}

}

```

---

<div class="post-metadata">

### Author: ![slow\_izzm](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/slow_izzm/32/5637_2.png) [@slow\_izzm](https://discourse.processing.org/u/slow_izzm)
#### Post date: [April 21, 2020, 4:19pm UTC](https://discourse.processing.org/t/how-do-i-make-doubleclicked-run-without-call-existing-mousepressed-p5-js/19982/5 "2020-04-21T16:19:22Z")

</div>

the `mousePressed()` is always going to be called … you could use a `setTimeout()` to check if `doubleClicked()` has been invoked.

```auto
function doubleClicked() {
  double = true;
}

function mousePressed() {
  setTimeout(_=> { double ? null : rectangle.pressed() }, 300);
  double = false;
}

```

---

<div class="post-metadata">

### Author: ![Royjan](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@Royjan](https://discourse.processing.org/u/Royjan)
#### Post date: [April 21, 2020, 5:55pm UTC](https://discourse.processing.org/t/how-do-i-make-doubleclicked-run-without-call-existing-mousepressed-p5-js/19982/6 "2020-04-21T17:55:13Z")

</div>

Thank you so much! I will try that!
