We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi, I've made a program where a truck moves back and forth. But when the truck moves the background where it has been turns black. I just wonder if it's my computer or if I need to change the code.
int rect1pos = 20;
int rect2pos = 80;
int rect3pos = 80;
int ell1pos = 30;
int ell2pos = 40;
int ell3pos = 95;
int step = 1;
void setup(){
size(300,300);
background(255);
}
void draw(){
line(0,150,300,150);
fill(255,0,0);
rect(rect1pos,105,60,40);
fill(255,0,0);
rect(rect2pos,125,25,20);
fill(255,0,0);
rect(rect3pos,115,15,10);
fill(0);
ellipse(ell1pos,145,10,10);
fill(0);
ellipse(ell2pos,145,10,10);
fill(0);
ellipse(ell3pos,145,10,10);
rect1pos = rect1pos+step;
rect2pos = rect2pos+step;
rect3pos = rect3pos+step;
ell1pos = ell1pos+step;
ell2pos = ell2pos+step;
ell3pos = ell3pos+step;
if(rect2pos > 275){
step = -1;
}
if(rect1pos < 0){
step = 1;
}
}
Answers
The draw method is executed about 60 frames per second so what is happening is that the black mark is a previous drawing of the truck. You need to clear the display at the start of the draw method
void draw(){ background(255); // white background
followed by the rest of your draw code.
Thanks!