# I don't get what's wrong with my code (move rect)

**URL:** <https://discourse.processing.org/t/i-dont-get-whats-wrong-with-my-code-move-rect/34509>\
**Category:** Coding Questions\
**Created:** [January 7, 2022, 6:52pm UTC](https://discourse.processing.org/t/i-dont-get-whats-wrong-with-my-code-move-rect/34509 "2022-01-07T18:52:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexandre\_Normandin](https://avatars.discourse-cdn.com/v4/letter/a/bbce88/32.png) [@Alexandre\_Normandin](https://discourse.processing.org/u/Alexandre_Normandin)\
**Post date:** [January 7, 2022, 6:52pm UTC](https://discourse.processing.org/t/i-dont-get-whats-wrong-with-my-code-move-rect/34509/1 "2022-01-07T18:52:40Z")

</div>

## hi, I’m just tarting to learn to code and I want my initial rectangle to move right when my mouse is clicked and I can’t seem to get the code right and I just can’t figure out why. If someone could please run it and tell me what I’m doing wrong that would be great. Thanks! Here is my code:

let col;  
let rectX;  
//let col2  
function setup() {  
createCanvas(600, 400);  
col = map(mouseX, 250, 350, 0, 255);  
//col2=map(mouseY,150,250,0,255)  
rectX = 300;  
}

function draw() {  
background(0);  
stroke(255);  
strokeWeight(4);  
noFill();  
if (mouseX \> 250 && mouseX \< 350 && mouseY \< 250 && mouseY \> 150) {  
fill(col, 0, 0);  
}  
rectMode(CENTER);  
rect(rectX, 200, 100, 100);  
rectX = 300;  
function mousePressed() {  
if (mouseX \> 250 && mouseX \< 350 && mouseY \< 250 && mouseY \> 150) {  
fill(255, 0, 0);  
rectMode(CENTER);  
rect(rectX, 200, 100, 100);  
rectX = rectX + 0.1  
;  
}  
}  
col = map(mouseX, 250, 350, 0, 255);  
}

---

<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:** [January 7, 2022, 7:06pm UTC](https://discourse.processing.org/t/i-dont-get-whats-wrong-with-my-code-move-rect/34509/2 "2022-01-07T19:06:31Z")

</div>

Hello, and welcome to the forum!

Great to have you here!

**Remarks**

`function mousePressed() ` was inside draw(). Bad

Also you had `rectX = 300;` in draw() (and not only in `setup()`) so it reseted `rectX` every time. Bad.

A change of + 0.1 might be too small. I changed this to `+5`.

**Full Sketch**

```auto
let col;
let rectX;
//let col2

function setup() {
  createCanvas(600, 400);
  col = map(mouseX, 250, 350, 0, 255);
  //col2=map(mouseY,150,250,0,255)
  rectX = 300;
}

function draw() {
  background(0);
  stroke(255);
  strokeWeight(4);
  noFill();
  if (mouseX > 250 && mouseX < 350 && mouseY > 150 && mouseY < 250) {
    fill(col, 0, 0);
  }
  rectMode(CENTER);
  rect(rectX, 200, 100, 100);
  // rectX = 300;

  col = map(mouseX, 250, 350, 0, 255);
}

function mousePressed() {
  if (mouseX > 250 && mouseX < 350 && mouseY < 250 && mouseY > 150) {
    //fill(255, 0, 0);
    //rectMode(CENTER);
    //rect(rectX, 200, 100, 100);
    rectX = rectX + 5;
  }
}

```

---

<div class="post-metadata">

**Author:** ![Alexandre\_Normandin](https://avatars.discourse-cdn.com/v4/letter/a/bbce88/32.png) [@Alexandre\_Normandin](https://discourse.processing.org/u/Alexandre_Normandin)\
**Post date:** [January 7, 2022, 9:55pm UTC](https://discourse.processing.org/t/i-dont-get-whats-wrong-with-my-code-move-rect/34509/3 "2022-01-07T21:55:25Z")

</div>

Thanks for the response.  
I wasn’t clear about it but I want the rectangle to move continuously when I moussePressed() rather than by a distance each time I press.

---

<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:** [January 7, 2022, 9:58pm UTC](https://discourse.processing.org/t/i-dont-get-whats-wrong-with-my-code-move-rect/34509/4 "2022-01-07T21:58:57Z")

</div>

```auto

let col;
let rectX;
let rectIsMoving=false;
//let col2

function setup() {
  createCanvas(600, 400);
  col = map(mouseX, 250, 350, 0, 255);
  //col2=map(mouseY,150,250,0,255)
  rectX = 300;
}

function draw() {
  background(0);
  stroke(255);
  strokeWeight(4);
  noFill();
  if (mouseX > 250 && mouseX < 350 && mouseY > 150 && mouseY < 250) {
    fill(col, 0, 0);
  }
  rectMode(CENTER);
  rect(rectX, 200, 100, 100);
  if(rectIsMoving)
   rectX += 3;

  col = map(mouseX, 250, 350, 0, 255);
}

function mousePressed() {
  if (mouseX > 250 && mouseX < 350 && mouseY < 250 && mouseY > 150) {
    //fill(255, 0, 0);
    //rectMode(CENTER);
    //rect(rectX, 200, 100, 100);
    rectIsMoving=true; 
  }
}

```

---

<div class="post-metadata">

**Author:** ![Alexandre\_Normandin](https://avatars.discourse-cdn.com/v4/letter/a/bbce88/32.png) [@Alexandre\_Normandin](https://discourse.processing.org/u/Alexandre_Normandin)\
**Post date:** [January 8, 2022, 12:50am UTC](https://discourse.processing.org/t/i-dont-get-whats-wrong-with-my-code-move-rect/34509/5 "2022-01-08T00:50:04Z")

</div>

thank you lots, very much appreciated
