How to make a very basic animation?

edited April 2016 in Questions about Code

Hi there. I'm very new to Processing and coding in general (currently taking an introductory course), and had a question about making a basic animation with for loops. So basically, I'm attempting to make a colored rectangle shift across the screen and restart back at the left side of the screen once its reached the right side. I've tried executing something along these lines

size (500, 500); //creating canvas
for (int i = 0; i < 500; i = i+1) { //feebly attempting to create animation
fill (153); //adding fill, not exactly sure where to put it
rect (i, 250, 15, 5); //creating rectangle using int variable i
noFill(); //end fill
} //fin

But, as I'm sure you could already tell, that was an absolute failure. I'm going to keep throwing everything at the wall to see what will stick, but any and all help would be appreciated. Muchas gracias in advance.

Tagged:

Answers

  • Answer ✓

    The screen is only updated at the very end of draw(), so you can't use a for loop here

    Instead declare i before setup and increase it in draw(): i=i+1;

    This works because draw loops in itself all the time automatically

  • Answer ✓

    Now once you got this use if(...

    to reset i to 0

    Better name is rectangleX btw.

  • Thank you so much man, very appreciated.

Sign In or Register to comment.