如何解决处理 - 将 hitTest 和 mousePressed 添加到具有 100 个球的动画中
我想创建一个球对象的 ArrayList,它应该在循环中直到有 100 个。
现在我的问题是:我必须实现一个函数 hitTest
,这样当你点击一个球时它就会被移除。在同一个位置上,应该会出现两个球,它们朝不同的方向。
有人可以帮我吗?我很失落...
这是我目前的代码:
Ball b;
ArrayList<Ball> balls;
void setup() {
size(800,800);
balls = new ArrayList<Ball>();
for (int i = 0; i<100; i++) {
drawBall();
}
}
void draw() {
background(255);
//b.update();
for (int i= 0; i<balls.size(); i++) {
balls.get(i).update();
}
}
void drawBall() {
Ball b = new Ball();
balls.add(b);
}
class Ball {
private float x;
private float y;
private float ballSize;
private float dirX;
private float dirY;
private boolean moving = true;
Ball() {
this.x = width/2;
this.y = height/2;
this.ballSize = random(10.0,30.0);
this.dirX = random(-3.0,3.0);
this.dirY = random(-3.0,3.0);
if (this.dirX<1.0 && this.dirX>1.0) //1 statt -1 macht zufälliger { this.dirX = 1.0; }
if (this.dirY<1.0 && this.dirY>1.0) {
this.dirY = 1.0;
}
}
public void update() {
stroke(255);
fill(random(255),random(255),random(255));
ellipse( this.x,this.y,this.ballSize,this.ballSize);
if (this.moving == true) {
this.x += this.dirX;
this.y += this.dirY;
}
if (this.x+ this.ballSize/2> width ||this.x- this.ballSize/2<0) {
this.dirX= dirX*-1;
}
if (this.y+ this.ballSize/2> height ||this.y- this.ballSize/2<0) {
this.dirY= dirY*-1;
}
}
}
解决方法
将您的问题分解为更小、更简单的步骤。
例如
当您点击一个球时,它会被移除。在相同的位置,应该会出现两个球,然后朝着不同的方向。
- 当您点击一个球时:您可以将 dist() 函数(检查鼠标和球之间的距离是否小于半径)与 mouseClicked()(或 {{1} } /
mousePressed()
) - 它被移除了:您已经调用了
mouseReleased()
。同样,您可以调用balls.add()
传递球对象或索引以删除(视情况而定) - 相同位置:您需要记住(存储坐标)被点击以在同一位置添加两个球的球
- 不同的方向:您已经在
balls.remove()
构造函数中这样做了:您可以在每个新球上应用相同的逻辑。
这是说明第 1 点的基本草图,使用 Ball()
:
dist()
将其粘贴到新草图中,运行它,您应该会掌握在问题上下文中使用 void draw(){
background(255);
int ballX = 50;
int ballY = 50;
int ballRadius = 35;
if(dist(ballX,ballY,mouseX,mouseY) < ballRadius){
fill(0,192,0);
}else{
fill(192,0);
}
ellipse(ballX,ballRadius * 2,ballRadius * 2);
}
的窍门。
关于第 2、3、4 点,这是草图的修改版本,带有注释和略有不同的方法:不是移除一个球以在不同方向的确切位置添加一个新球,只需随机化方向即可。从视觉上看,它将类似于一个新球(随机大小/颜色除外)。随着被点击的球被重复使用,只添加了第二个:
dist()
Ball b;
ArrayList<Ball> balls;
void setup() {
size(800,800);
balls = new ArrayList<Ball>();
for (int i = 0; i<100; i++) {
drawBall();
}
}
void draw() {
background(255);
//b.update();
for (int i= 0; i<balls.size(); i++) {
// pass the mouse coordinates to each ball to check if it's hovered or not
balls.get(i).update(mouseX,mouseY);
}
}
// on mouse pressed
void mousePressed(){
for (int i= 0; i<balls.size(); i++) {
// make current ball reusable in this loop
Ball ball = balls.get(i);
// if ball is hovered
if(ball.isHovered){
// randomize direction of current ball
ball.setRandomDirection();
// add a new ball from the current location
balls.add(ball.copy());
}
}
}
void drawBall() {
Ball b = new Ball();
balls.add(b);
}
class Ball {
private float x;
private float y;
private float ballSize;
private float dirX;
private float dirY;
private boolean moving = true;
private color fillColor;
// property to keep track if the ball is hovered or not
private boolean isHovered;
Ball() {
this.x = width/2;
this.y = height/2;
this.ballSize = random(10.0,30.0);
this.setRandomDirection();
this.fillColor = color(random(255),random(255),random(255));
}
// extract random direction calls into a re-usable function (handy for click / collision)
void setRandomDirection(){
this.dirX = random(-3.0,3.0);
this.dirY = random(-3.0,3.0);
if (this.dirX<1.0 && this.dirX>1.0) { //1 statt -1 macht zufälliger { this.dirX = 1.0; }
if (this.dirY<1.0 && this.dirY>1.0) {
this.dirY = 1.0;
}
}
}
public void update(int x,int y) {
// euclidean distance between this ball's coordinates a given x y position (e.g. mouse)
isHovered = dist(this.x,this.y,x,y) < this.ballSize / 2;
// optional: use stroke color to visually display highlighted ball
if(isHovered){
stroke(0);
}else{
stroke(255);
}
fill(fillColor);
ellipse( this.x,this.ballSize,this.ballSize);
if (this.moving == true) {
this.x += this.dirX;
this.y += this.dirY;
}
if (this.x + this.ballSize / 2 > width ||
this.x - this.ballSize / 2 < 0) {
this.dirX= dirX*-1;
}
if (this.y + this.ballSize / 2 > height ||
this.y - this.ballSize / 2 < 0) {
this.dirY= dirY*-1;
}
}
// utility function: simply copies this ball's x,y position to the new one
Ball copy(){
Ball clone = new Ball();
clone.x = this.x;
clone.y = this.y;
return clone;
}
}
方法足够灵活,如果绝对必须,可以轻松地移除一个球来添加两个球。例如:
copy()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。