# Dragging and dropping an object

**URL:** https://discourse.processing.org/t/dragging-and-dropping-an-object/11917
**Category:** Coding Questions
**Created:** [June 8, 2019, 7:10am UTC](https://discourse.processing.org/t/dragging-and-dropping-an-object/11917 "2019-06-08T07:10:33Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![ZachMcMkay](https://avatars.discourse-cdn.com/v4/letter/z/8e7dd6/32.png) [@ZachMcMkay](https://discourse.processing.org/u/ZachMcMkay)
#### Post date: [June 8, 2019, 7:10am UTC](https://discourse.processing.org/t/dragging-and-dropping-an-object/11917/1 "2019-06-08T07:10:33Z")

</div>

Hello! im trying to create a game where i can pick up a piece move it and then drop it where i want. I’ve run into a wall trying to logically think about dropping the piece.

in order to pick it up and move it by having it follow the mouse I did this.

```auto
function mousePressed(){
	for (let i = 0; i < pieces.length; i++){
		pieces[i].contains(mouseX, mouseY);
	}

```

then in my class for the piece i have this.

```auto
move(x,y){
    if (this.selected){
      this.x = x;
      this.y = y;
    }
  }

  contains(x,y){
    if (x > this.x - 30 && x < this.x + 25 && y > this.y - 30 && y < this.y + 25) {
      this.selected = true;
    }
  }

```

Where i call move i pass in mouseX, and mouseY as the arguments. So it works perfectly… I’ve run into a wall trying to then let the piece go and stay where i want it… My code atm is long so i’ve created a more basic example. Thanks for the help!

[https://editor.p5js.org/zachmcmkay/sketches/Wdg04mmFi](https://editor.p5js.org/zachmcmkay/sketches/Wdg04mmFi)

---

<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: [June 8, 2019, 7:20am UTC](https://discourse.processing.org/t/dragging-and-dropping-an-object/11917/2 "2019-06-08T07:20:06Z")

</div>

you could try like:

```auto
function mouseReleased() {
	for (let i = 0; i < pieces.length; i++){
		pieces[i].selected = false;
	}
}

```

---

<div class="post-metadata">

### Author: ![ZachMcMkay](https://avatars.discourse-cdn.com/v4/letter/z/8e7dd6/32.png) [@ZachMcMkay](https://discourse.processing.org/u/ZachMcMkay)
#### Post date: [June 8, 2019, 7:22am UTC](https://discourse.processing.org/t/dragging-and-dropping-an-object/11917/3 "2019-06-08T07:22:12Z")

</div>

I had no idea mouseReleased was a function… i missed that on the reference page. Thanks man!
