At its most simple, the easiest way to move a robot is with translate(x,y). But, your robot needs to be in an active sketch, not in a static one. That means you'll need to set up void setup() and void draw(), then put the code that draws the robot into the draw() function/loop.
PROCESSING
Simple Movement
// Simple Movement
// Setup
void setup() {
size(500,500);
}
// Draw
void draw() {
// Draw the robot (as many lines of code as needed)
rect(200,200,200,200);
}
That puts the robot (in this case, a simple ellipse) into the draw function/loop so it can be moved. Next, we'll add a translate(x,y) command to move the robot along the x access. We do this by making the x position a variable, newX, and adding a bit to it each time the draw loop/function runs.
PROCESSING
Simple Movement 2
// Simple Movement
// Variable for the horizontal (x) location
int newX = 0;
// Setup
void setup() {
size(500,500);
}
// Draw
void draw() {
// Refresh the background so there's no trails
background(200);
// Translate the robot
translate(newX,0);
// Increase the value of newX
newX = newX + 1;
// Check the value of newX
println(newX);
// Draw the robot (as many lines of code as needed)
rect(200,200,200,200);
}
That's all well and good, but the robot dashes off to the right and never comes back. To prevent that from happening, we check the value of newX, which moves the robot right, to see if it's past the right edge. And if it is (a classic use of an if/then statement) we reset the value of newX so the robot is just off the left edge of the window. Then, newX still increases each time the draw() loop/function is run, but with the value of newX starting over.
PROCESSING
Simple Movement + Wrap
// Simple Movement + Wrap
// Variable for the horizontal (x) location
int newX = 0;
// Setup
void setup() {
size(500,500);
}
// Draw
void draw() {
// Refresh the background so there's no trails
background(200);
// Translate the robot
translate(newX,0);
// Increase the value of newX
newX = newX + 1;
// Check the value of newX
println(newX);
// Reset newX if the robot is off the right edge of the screen
if(newX > 300) {
newX = -400;
}
// Draw the robot (as many lines of code as needed)
rect(200,200,200,200);
}