We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hey,
Just wondering if anyone can help me out with this bit of code.
I'm trying to remove an ellipse that's drawn to the screen after I've clicked it. Not sure how to approach the problem?
ArrayList <ball> balls = new ArrayList <ball>();
int numOfBalls = 5;
float dist;
class ball {
float x, y;
ball() {
x = random(20, width-20);
y = random(20, height-20);
}
void draw_ball () {
ellipse(x, y, 20, 20);
}
void remove_ball () {
for (int i = numOfBalls-1; i >=0; i--) {
dist = sqrt((x-mouseX) * (x-mouseX) + (y-mouseY) * (y-mouseY));
if (dist <= 20) {
balls.remove(i);
}
}
}
}
void setup () {
size(500, 500);
for ( int i = 0; i < numOfBalls; i++)
balls.add(new ball());
}
void draw () {
for ( int i = 0; i < numOfBalls; i++)
balls.get(i).draw_ball();
}
void mousePressed() {
remove_ball();
}
Answers
In mousePressed for loop again over all ellipses but backward see reference on ArrayList
Use dist to determine whether the ellipse is close mouse; if so use remove
Sorry i'm new to programming and I don't really know what i've done wrong >__<
If I move the remove ball code to mouse pressed then I get errors
You need to be more specific than just saying I've got errors.
Which 1? Which line has it occurred? =;
Much probably the compiling error is about there are no x nor y variables within mousePressed(). 8-X
The only x & y in your sketch are instance fields belonging to class ball. #-o
BtW, classes & interfaces should follow the UpperCase naming convention.
So it shoulda been named Ball rather than ball. :-B
Now for a much cleaner solution: [-O<
Let's declare 2 constants for your now Ball class, named DIAM & RAD.
They're respectively the diameter & radius for Ball's ellipse():
After that, let's implement a new method to check whether the mouse's pointer is inside the Ball's ellipse().
That is, point/circle collision check: http://JeffreyThompson.org/collision-detection/point-circle.php
Finally we can rewrite sketch's mousePressed() to take advantage of Ball's new method: :-bd
Thanks so much for the help ^:)^
Just one more question,
This code is void, however you're returning a value? Is this supposed to be the case? as i'm getting an error when I copy it into my Ball class.
Oops! Forgot to declare that method returns a
boolean
value due to<
operator. Sorry, fixed now! b-(https://Processing.org/reference/boolean.html
https://Processing.org/reference/lessthan.html
Ughhh sorry i'm so nooby :|
I'm getting a 'index out of bounds exception index 4 size 4' error when I click an ellipse.
Within draw(), at its
for ( ; ; )
loop, the conditional is checking against numOfBalls.However, once remove() is executed, numOfBalls doesn't match balls's size() anymore: :|
http://docs.Oracle.com/javase/8/docs/api/java/util/Collection.html#size--
My proposal fix is to use an enhanced
for ( : )
instead of a regularfor ( ; ; )
loop: L-)https://Processing.org/reference/for.html
So you don't need to worry about balls's current size() there: *-:)
Thanks so much man @-)
It's all working spick and span now :>
Posted your full & refactored sketch online. Check it out at the link below: :bz
http://studio.SketchPad.cc/sp/pad/view/ro.PKnDDqu1vYo/latest
Bonus sketch: a p5.js version too: :-bd
http://p5js.SketchPad.cc/sp/pad/view/ro.CaQVvhrEKxAeXd/latest
Awesome :P