Arraylist - Storing Positions
in
Programming Questions
•
1 year ago
Hi there.
Following the arraylist example (found here
http://processing.org/learning/topics/arraylistclass.html), I use the code that I want to create scrolling rectangular shapes that each moves in a different position.
First mouse click move the first rectangular from 0 to 150 (Y axis).
Second mouse click move the first rectangular from 150 to 300, AND the second rectangular from 0 to 150.
And so on.
I can't find how and where to set the position variables.
Can anyone help please???
- ArrayList balls;
- int ballWidth = 48;
- void setup() {
- size(200, 200);
- smooth();
- noStroke();
- balls = new ArrayList();
- }
- void draw() {
- background(255);
- for (int i = balls.size()-1; i >= 0; i--) {
- Ball ball = (Ball) balls.get(i);
- ball.move();
- ball.display();
- if (balls.size() > 3) {
- balls.remove(0);
- }
- }
- }
- void mousePressed() {
- balls.add(new Ball(10, 0, ballWidth));
- }
- class Ball {
- float x;
- float y;
- float speed;
- float gravity;
- float w;
- float life = 255;
- Ball(float tempX, float tempY, float tempW) {
- x = tempX;
- y = tempY;
- w = tempW;
- speed = 0;
- gravity = 0.1;
- }
- void move() {
- speed = speed + gravity;
- y = y + speed;
- if (y > height-w) {
- y = height-w;
- }
- }
- boolean finished() {
- life--;
- if (life < 0) {
- return true;
- } else {
- return false;
- }
- }
- void display() {
- fill(0);
- rect(x,y,width-20,w);
- }
- }
1