Making a class move

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);
}

}

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:

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.

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.

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?

Did you call move () from draw()?

Show your code

You want this also in draw()

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

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

1 Like

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

1 Like

this is my version

// 
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
//