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!
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?