Animation using Shapes

edited November 2013 in Questions about Code

Hii there. I am just trying to create a simple animation using 2 shapes ellipse and rectangle. Both of them having the same center point are asked to move in opposite direction, but while in motion the circle leaves behind a trail which i want to get it removed.Please help.

The code is :

PVector location1 = new PVector(500,500);
PVector location2 = new PVector(500,500);
PVector velocity1 = new PVector(0.2,0);
PVector velocity2 = new PVector(-0.2,0);

void setup() {
 size(800,700);
 background(0);
String a = "Type Initial letters of the shapes(Rectangle, Ellipse or Line) you want, then press enter to start the motion";
   text(a,200,100);
   ellipseMode(CENTER);
 rectMode(CENTER);

 }

void draw() {
  stroke(255);

  fill(0);

  if(key == 'e')                                       // select your own shapes
     ellipse(location1.x, location1.y,250,250);
  if(key == 'r')
     rect(location2.x, location2.y,150,75);
  //if(key == 'l')
  //  triangle(

  if(key == 10){
   if(location1.x<525){
      location1.add(velocity1);
      ellipse(location1.x, location1.y, 250, 250);
   }
   if(location2.x>475){
      location2.add(velocity2);
   rect(location2.x,location2.y,150,75);
   }

}

ellipseMode(CENTER);
fill(255);
ellipse(500,500,2,2);


}

Answers

  • Answer ✓

    The fundamental change that you need to make is to move the background() call to draw(). You will also need to display your text in draw()...

  • Answer ✓

    First of all, when posting code, highlight it and hit ctrl-k to make it into a more readable format. To answer your question: When you draw anything in Processing, you set the pixels to be a certain color, and they stay set to that color until you re-assign them a color. The correct way to do it in your program is to reset the background every time you go through the draw() function.

    Check out the background() function in the Processing reference for more help.

  • When i move background() to draw(), only one shape shows up, the other gets hidden for some reason. Same is with the text

  • Sorry for the mess up in the code, just forgot to make it legible. I agree with your answer but the hidden shapes persists. what about that?

  • background() should be your first call in draw(). Again, you'll need to move your text() call to draw() as well.

    Also: I cleaned up your code...

  • Thanks calsign

Sign In or Register to comment.