# Function within mousePressed function doesn't work

**URL:** https://discourse.processing.org/t/function-within-mousepressed-function-doesnt-work/40981
**Category:** Coding Questions
**Created:** [February 21, 2023, 7:23pm UTC](https://discourse.processing.org/t/function-within-mousepressed-function-doesnt-work/40981 "2023-02-21T19:23:05Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![MaxU89](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@MaxU89](https://discourse.processing.org/u/MaxU89)
#### Post date: [February 21, 2023, 7:23pm UTC](https://discourse.processing.org/t/function-within-mousepressed-function-doesnt-work/40981/1 "2023-02-21T19:23:05Z")

</div>

Hello. I am still kind of new to processing. I have been trying to create a game with moving platform(I called it plank), balls which shoot down bricks. I am currently creating the platform and I want it to move on the x axis with my cursor when my mouse is pressed, but it does not. I would really appreciate help.

```auto
Plank myPlank;

void setup(){
  size(500,500);
  myPlank = new Plank();
}
  

void draw(){
  background(0);
  myPlank.display();
}

class Plank{
  int xpos;
  int ypos;
  int l;
  int w;
  int col;
  
  Plank(){
   xpos = width/2;
   ypos = height-(height*1/5);
   l = width/10;
   w = height/100;
   col = 255;
  }
  
   void display() {
     rectMode(CENTER);
     fill(col);
     rect(xpos,ypos,l,w);
   }
   void mousePressed(){
     myPlank.move();     
   }
   void move(){
     xpos = mouseX;
     if(xpos<0+l/2||xpos>width-l/2){
       xpos+=l/2;
     }
   }
}

```

---

<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: [February 21, 2023, 7:27pm UTC](https://discourse.processing.org/t/function-within-mousepressed-function-doesnt-work/40981/2 "2023-02-21T19:27:22Z")

</div>

> [@MaxU89](#):
>
> mousePressed

This inside the class won’t work

It’s hidden inside the class

Make a mousePressed outside the class and call the function mousePressed that is in the class from there

---

<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: [February 21, 2023, 8:40pm UTC](https://discourse.processing.org/t/function-within-mousepressed-function-doesnt-work/40981/3 "2023-02-21T20:40:53Z")

</div>

**Remark**

Other than

```auto
void mousePressed() {
  myPlank.move();
}

```

we want a mousePressed that works throughout and not only once every click.

This is done with a variable of the same name mousePressed (the name that also the function has) :

```auto
void draw() {
  background(0);
  myPlank.display();

  if (mousePressed) {
    myPlank.move();
  }
}

```

**Remark**

not sure what you do here, but add something to the pos is different for left and right side.

```auto
    if (xpos<0+l/2||xpos>width-l/2) {
      xpos+=l/2;

```

maybe:

```auto
if (xpos<0+l/2) {
      xpos+=l/2; // left side 
}
if (xpos>width-l/2) {
      xpos-=l/2; // right side 
}

```

(I did not work on this further)

**Sketch**

```auto

Plank myPlank;

void setup() {
  size(500, 500);
  myPlank = new Plank();
}

void draw() {
  background(0);
  myPlank.display();

  if (mousePressed) {
    myPlank.move();
  }
}

// ============================================================================

class Plank {
  int xpos;
  int ypos;
  int l;
  int w;
  int col;

  Plank() {
    xpos = width/2;
    ypos = height-(height*1/5);
    l = width/10;
    w = height/100;
    col = 255;
  }

  void display() {
    rectMode(CENTER);
    fill(col);
    rect(xpos, ypos, l, w);
  }

  void move() {
    xpos = mouseX;
    if (xpos<0+l/2||xpos>width-l/2) {
      xpos+=l/2;
    }
  }
}
//

```

---

<div class="post-metadata">

### Author: ![MaxU89](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@MaxU89](https://discourse.processing.org/u/MaxU89)
#### Post date: [February 21, 2023, 8:55pm UTC](https://discourse.processing.org/t/function-within-mousepressed-function-doesnt-work/40981/4 "2023-02-21T20:55:50Z")

</div>

Appreciate your help. Everything is working now. Thanks 😉
