How to animate bars/columns vertically.
in
Programming Questions
•
4 months ago
Hi,
I'm trying to figure out how to consolidate drawing these lines into a for-loop in the draw() function. Basically, I'm trying to animate a bar graph, eg trying to draw each bar from 0 to it's Y value, and then it draws the next bar and so on. I can manually add each block below, and it works fine to create each line, but it seems better to automate it. Each line creates a bar/column.
- float yPos = map(actions[1].presses, minVal, maxVal, height/2, 50);
- line(lineXpos, actions[1].a, endLineXpos, actions[1].a);
- actions[1].a = actions[1].a - 2;
- if (actions[1].a < yPos) {
- actions[1].a = height/2;
- }
- float yPos2 = map(actions[2].presses, minVal, maxVal, height/2, 50);
- line(lineXpos+10, actions[2].a, endLineXpos+10, actions[2].a);
- actions[2].a = actions[2].a - 2;
- if (actions[2].a < yPos2) {
- actions[2].a = height/2;
- }
- float yPos3 = map(actions[3].presses, minVal, maxVal, height/2, 50);
- ...
- ...
- and so on...
It seems like I could consolidate the above into something like this below, but when I run it, it runs endlessly and creates a slope.
- void draw() {
- for( int j = 1; j < actions.length; j++) {
- actions[j].yPos = map(actions[j].presses, minVal, maxVal, height/2, 50);
- line(actions[j].lineXpos, actions[j].a, actions[j].endLineXpos, actions[j].a);
- actions[j].a = actions[j].a - 2;
- if (actions[j].a < actions[j].yPos) {
- actions[j].a = height/2;
- actions[j].lineXpos = actions[j].lineXpos + 10;
- actions[j].endLineXpos = actions[j].endLineXpos + 10;
- }
- }
- }
I think the actions[j].yPos gets overwritten each time. Any ideas what I'm doing wrong?
Thanks!
1