Assign value once to every object
in
Programming Questions
•
1 year ago
Hello,
I working on a simple program as a part of a sound-reactive installation. Every time the sound velocity is more than 5, a rectangle comes in on the right, and all other objects shift one place to the left.
The problem: the velocity is the width/height of the rectangles. Now it affects all objects, but I only want to assign the value to the newest, so all rectangles get their own size (and stay that size). Maybe there is something wrong with void show() and I tried things with an arraylist, but I'm afraid I'm a bit lost.
This is a simplified example to make it more clear, in the real version the rectangles are characters (kind of sound-reactive newsticker).
If anyone can point me in the right direction, that would be great :-)
- import ddf.minim.*;
- Minim minim;
- AudioInput in;
- AudioRecorder recorder;
- Rectangle[] myRectangle;
- float[] vs = new float[1000];
- float vel;
- int count;
- int index;
- public float y;
- private float v;
- String txt;
- void setup() {
- size(1024, 400);
- noStroke();
- minim = new Minim(this);
- in = minim.getLineIn(Minim.STEREO, 2048);
- count = 50;
- char[] allChars = new char[count];
- myRectangle = new Rectangle[count];
- for (int i = 0; i < count; i++) {
- myRectangle[i] = new Rectangle();
- }
- }
- void draw() {
- y = in.left.get(2)*800;
- scale(0.1);
- background(255);
- translate(0, 200);
- if (y>5) {
- show();
- }
- for (int i=0;i<count;i++) {
- vel = y;
- myRectangle[i].display();
- }
- }
- void show() {
- if (index<count) {
- for (int i=0;i<index;i++) {
- vel = y;
- myRectangle[i].update();
- }
- }
- index++;
- }
class
- class Rectangle {
- float xpos;
- Rectangle() {
- xpos = width*10;
- }
- void display() {
- fill(0, 99, 220);
- rectMode(CENTER);
- rect(xpos+40, 0, vel, vel);
- }
- void update() {
- int spacing = 300;
- xpos = xpos - spacing;
- }
- }
1