Grabbing individual circles
in
Programming Questions
•
2 years ago
So I have some code that uses some "lighting" to see through the first layer into the second layer, but I'm having trouble with allowing the user to grab a light. I thought I could set it under the conditional if statement that determines whether the lights move while the mouse is over them, as it functions only on the lights that the mouse is currently over. However, instead, whenever the mouse is pressed, it grabs all the lights. So, what I thought I understood, I don't. How is the first if statement only impacting one light at a time and not the second? How can I grab only one light at a time?
- ArrayList <Lighting> works = new ArrayList <Lighting> ();
- int spotlights = 25;
- float maxSpeed = 2.25;
- int circleRadius = 100;
- PGraphics lightingMask;
- void setup () {
- size(900, 600);
- noStroke();
- smooth();
- for (int i=0; i<spotlights; i++) {
- works.add(new Lighting(random(circleRadius, width-circleRadius), random(circleRadius, height-circleRadius), circleRadius));
- }
- textFont(createFont("Arial", 50));
- lightingMask = createGraphics(width, height, JAVA2D);
- lightingMask.beginDraw();
- lightingMask.noStroke();
- lightingMask.smooth();
- lightingMask.endDraw();
- }
- void draw() {
- background(10);
- fill(255);
- textAlign(CENTER, CENTER);
- textSize(50);
- text("Essence of Anthem", 20, 20, width-40, height-40);
- fill(255);
- textAlign(CENTER, BOTTOM);
- textSize(20);
- text("Excerpts from Ayn Rand's Anthem", 20, 20, width-40, height-40);
- PImage backgroundImage = get();
- lightingMask.beginDraw();
- lightingMask.background(0);
- for (Lighting w : works) {
- w.update();
- w.display();
- }
- lightingMask.endDraw();
- background(0);
- backgroundImage.mask(lightingMask);
- image(backgroundImage, 0, 0);
- }
- class Lighting {
- float x, y, speedX, speedY, transparency;
- int radius;
- Lighting (float tempX, float tempY, int tempRadius) {
- x = tempX;
- y = tempY;
- radius = tempRadius;
- while (abs (speedX) < 0.25) { speedX = random(-maxSpeed, maxSpeed); }
- while (abs (speedY) < 0.25) { speedY = random(-maxSpeed, maxSpeed); }
- transparency = random(50, 255);
- }
- void update () {
- if (dist(x,y,mouseX,mouseY) > radius/2)
- {
- x += speedX;
- y += speedY;
- //by setting it here, I thought it would behave similar
- //limiting the behavior to only those circles that apply
- //under the initial if statement
- if(mousePressed)
- {
- x= mouseX;
- y= mouseY;
- }
- }
- if (x < radius/2 || x > width-radius/2) { speedX = -speedX; }
- if (y < radius/2 || y > height-radius/2) { speedY = -speedY; }
- }
- void display() {
- lightingMask.fill(255, transparency);
- lightingMask.ellipse(x, y, radius, radius);
- }
- }
1