# Making a class move

**URL:** <https://discourse.processing.org/t/making-a-class-move/29571>\
**Category:** Coding Questions\
**Created:** [April 23, 2021, 1:47am UTC](https://discourse.processing.org/t/making-a-class-move/29571 "2021-04-23T01:47:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![chatters22](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chatters22/32/12908_2.png) [@chatters22](https://discourse.processing.org/u/chatters22)\
**Post date:** [April 23, 2021, 1:47am UTC](https://discourse.processing.org/t/making-a-class-move/29571/1 "2021-04-23T01:47:55Z")

</div>

I’m quite new to Processing, earlier when I wasn’t working with classes I could easily make my objects move with simple code like xLoc = xLoc + 1. But now I’m working with classes, and I can’t get them to move for some reason. Code such as void move() doesn’t seem to do anything. Please help!

Code:  
void setup() {  
size (750, 750);  
background(255);  
}

void draw() {

```
int xLoc =(int) 350;
int yLoc =(int) 350;

Player player = new Player(xLoc, yLoc);
player.drawPlayer();

```

xLoc = xLoc + 5;

}

Class:  
class Player {  
int xLoc;  
int yLoc;  
int playerSize;

Player(int tempX, int tempY)

{  
xLoc = tempX;  
yLoc = tempY;  
}

void drawPlayer() {  
fill(120, 200, 255);  
ellipse(xLoc, yLoc, 100, 100);  
}

}

---

<div class="post-metadata">

**Author:** ![Mattyjak](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mattyjak/32/16777_2.png) [@Mattyjak](https://discourse.processing.org/u/Mattyjak)\
**Post date:** [April 23, 2021, 2:21am UTC](https://discourse.processing.org/t/making-a-class-move/29571/2 "2021-04-23T02:21:35Z")

</div>

From the code snippet you’ve submitted, there doesn’t seem to be any movement function created inside the class at all. The class object `player` won’t move on its own as no instructions to move anywhere have been given.

What I’d recommend doing:

```auto
class Player {
  int xLoc;
  int yLoc;
  int playerSize;
  int speed = 25;
  Player(int tempX, int tempY)
//Constructor
     {
        xLoc = tempX;
        yLoc = tempY;
}

void drawPlayer() {
fill(120, 200, 255);
ellipse(xLoc, yLoc, 100, 100);
//draws the player at the current position.
}
void move(destinationX,destinationY){
     if(xLoc > destinationX){
         xLoc-=speed;
     } else{
         xLoc+=speed;
     }
     if(yLoc > destinationY){
         yLoc-=speed;
     } else{
         yLoc+=speed;
     }
//Check if greater than or lower than destination position, and moves towards it. 
}

}

```

All I’m saying is that you need to have a function inside of the class to tell it how and ‘where’ to move.

Another thing I noticed, is that you are constantly calling the position of the class inside of the draw loop. It won’t move anywhere since it is basically just being told to go to 350, 350.

> [@chatters22](#):
>
> void draw() {
> 
> ```auto
> int xLoc =(int) 350;
> int yLoc =(int) 350;
> 
> Player player = new Player(xLoc, yLoc);
> player.drawPlayer();
> 
> ```
> 
> xLoc = xLoc + 5;
> 
> }

Create the player object outside of the draw loop, create a movement function, and stop telling it to go to 350,350 over and over again.

---

<div class="post-metadata">

**Author:** ![chatters22](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chatters22/32/12908_2.png) [@chatters22](https://discourse.processing.org/u/chatters22)\
**Post date:** [April 23, 2021, 3:05am UTC](https://discourse.processing.org/t/making-a-class-move/29571/3 "2021-04-23T03:05:35Z")

</div>

Thanks, this helps a lot! I have another problem though, even though I’ve moved the creation of the player object into setup so that isn’t constantly put to 350/350 and also created a void move() function which is just xLoc = xLoc + 10 as a test, nothing seems to happen. Is there something I’m missing?

---

<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 23, 2021, 4:58am UTC](https://discourse.processing.org/t/making-a-class-move/29571/4 "2021-04-23T04:58:25Z")

</div>

Did you call move () from draw()?

Show your code

---

<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 23, 2021, 4:59am UTC](https://discourse.processing.org/t/making-a-class-move/29571/5 "2021-04-23T04:59:49Z")

</div>

> [@chatters22](#):
>
> background(255);

You want this also in draw()

---

<div class="post-metadata">

**Author:** ![chatters22](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chatters22/32/12908_2.png) [@chatters22](https://discourse.processing.org/u/chatters22)\
**Post date:** [April 23, 2021, 11:07am UTC](https://discourse.processing.org/t/making-a-class-move/29571/6 "2021-04-23T11:07:41Z")

</div>

void setup() {  
size (750, 750);

```
int xLoc =(int) 350;
int yLoc =(int) 350;

Player player = new Player(xLoc, yLoc);
player.drawPlayer();

```

}

void draw() {  
background(255);  
}

class Player {  
int xLoc;  
int yLoc;  
int playerSize;  
int speed = 25;

Player(int tempX, int tempY)

{  
xLoc = tempX;  
yLoc = tempY;  
}

void drawPlayer() {  
fill(120, 200, 255);  
ellipse(xLoc, yLoc, 100, 100);  
}

void move(){  
xLoc = xLoc + 10;  
}  
}

Now I can’t see the ball at all. I’m not sure what I’ve done wrong

---

<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 23, 2021, 11:09am UTC](https://discourse.processing.org/t/making-a-class-move/29571/7 "2021-04-23T11:09:27Z")

</div>

> [@chatters22](#):
>
> ```auto
> int xLoc =(int) 350;
> int yLoc =(int) 350;
> 
> Player player = new Player(xLoc, yLoc);
> 
> ```

this must be before setup, so player is a global variable

setup() is run only once, so no use for `player.drawPlayer();`, move this into draw()

you must call move so it is executed, do this in draw(), too

---

<div class="post-metadata">

**Author:** ![chatters22](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chatters22/32/12908_2.png) [@chatters22](https://discourse.processing.org/u/chatters22)\
**Post date:** [April 23, 2021, 11:13am UTC](https://discourse.processing.org/t/making-a-class-move/29571/8 "2021-04-23T11:13:27Z")

</div>

I added in player.move(); and it worked! Thank you so much, this helped a lot

---

<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 23, 2021, 11:14am UTC](https://discourse.processing.org/t/making-a-class-move/29571/9 "2021-04-23T11:14:38Z")

</div>

this is my version

```auto
// 
Player player = new Player(350, 350);

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

void draw() {
  background(255);

  player.drawPlayer();
  player.movePlayer();
}

class Player {
  int xLoc;
  int yLoc;
  int playerSize;
  int speed = 25;

  Player(int tempX, int tempY) {
    xLoc = tempX;
    yLoc = tempY;
  }

  void drawPlayer() {
    fill(120, 200, 255);
    ellipse(xLoc, yLoc, 100, 100);
  }

  void movePlayer() {
    xLoc = xLoc + 10;
  }
}//class
//

```
