r/processing • u/MinecraftMagma • Mar 24 '25
Beginner help request I'm having trouble finding a way to change a value over a span of time.
What I've done is make a square go down by changing the value of Y for a square to simulate gravity; it's fairly simple.
The hard thing I'm stuck on is how I can get the Y value to change over time. I tried to use a loop, but I needed to define what the Y value was, and it always ended up teleporting to what I defined Y as.
I tried to set up an if statement, but I don't have a way to define what a distance between two points is to track and change (And I would need a loop for that, too).
I need help knowing how to make a change happen over time, and maybe a way to change how much time it takes for a change to happen.
5
u/TooLateForMeTF Mar 24 '25
Remember that Processing is busy doing a loop for you: it automatically calls draw() 30 times per second.
Change your Y value by a little bit, just once somewhere in your draw() routine. It will display whatever you're drawing, and then a fraction of a second later, Processing will invoke draw() again. At which point your code will once again change your Y value by a little bit and draw the updated result.
This is the mindset you have to get into if you want animation. Every draw() call should create the next "frame" of the animation. In fact, there's even a built-in frameCount variable you can access if you need to do something with that. In fact, try this: in your draw() routine, just set
y=frameCount; and see what that does.2
u/ChuckEye Mar 24 '25
There are (at least) two common ways.
One would be to have a counter, initialized in the setup, and then incremented in the draw loop.
The other would be to use one of the actual time functions like millis()
1
u/Legitimate_Handle_86 Mar 25 '25
I’m wondering if maybe you are declaring the starting y value in the draw function instead of setup? If you have y defined as a fixed value in the draw function it will reset it to that value every frame. If that is the case, you will need to first declare the existence of the y variable by using
int y;
before setup and draw so that both know it exists. Then declare the initial value inside of setup.
void setup(){ y = 0; }
then in the draw function, you can choose how it actually changes each frame.
void draw(){ y += 1; }