how to reset a class function variable
in
Programming Questions
•
1 year ago
I isolated this piece of code to make this question. I feel that there is an easy and obvious way to do this, but i just can't see it... So i came here for help.
How to reset the "amount" after color finished to change. I'd like the object to do this by it self. Is that possible?
Just click to change color, with line commented out, if you click again and again the color will not animate because the amount is already 1. If you uncomment the line inside mouseClicked(), will get the behaviour i'd like.
- ColorAnimator blender = new ColorAnimator();
- color c1 = color (255, 100, 100);
- color c2 = color ( 50, 240, 120);
- boolean animate =false;
- void setup()
- {
- size (300,300);
- background(c1);
- }
- void draw()
- {
- if (animate)
- background(blender.animate(c1, c2, 0.02));
- else
- background(c1);
- }
- void mouseClicked()
- {
- animate = !animate;
- //blender.amount = 0; // uncomment this to get the desired behavior
- }
- class ColorAnimator
- {
- float amount = 0;
- ColorAnimator()
- {}
- color animate(color c1, color c2, float speed)
- {
- color ccc;
- while(amount< 1.0)
- {
- ccc= lerpColor(c1,c2, amount);
- println(ccc);
- amount+=speed;
- return ccc;
- }
- return c2;
- }
- }// eof Animator class
1