简体   繁体   English

如何对ArrayList中的所有元素调用函数?

[英]How do I call a function to all elements inside my ArrayList?

I am making a program that draws a pattern/design using balls that bounce around the screen using Processing. 我正在制作一个程序,该程序使用处理在屏幕周围反弹的球来绘制图案/设计。 I have managed to get one ball, moving, drawing, and bouncing properly. 我设法使一个球正确地移动,绘画和弹跳。 However, once I made my ArrayList, and drawn all my balls on the screen using an Iterator, they stopped moving. 但是,一旦我创建了ArrayList,并使用Iterator在屏幕上绘制了所有球,它们便停止移动。

I'm not really sure what to do, I've tried calling move() in the iterators while loop, and tried calling it in the constructor of Ball() (don't really know if that does anything). 我不太确定该怎么做,我曾尝试在迭代器的while循环中调用move(),并尝试在Ball()的构造函数中调用它(不知道是否有任何作用)。 I only included the code that I think I was having problems with. 我只包含了我认为有问题的代码。

import java.util.ArrayList;
import java.util.Iterator;

class Ball {
    float x;
    float y;
    float directionDegree;
    float speed = 8;

    Ball() {
        x = random(0, 600);
        y = random(0, 600);
        directionDegree = random(60, 120);
    }

    void move() {
        x += speed * Math.cos(direction);
        y += speed * Math.sin(direction);
    }

    void drawAll(ArrayList<Ball> balls) {
        Iterator<Ball> iter = balls.iterator();

        while (iter.hasNext()) {
            iter.next().draw();
            move();
        }
    }
}

Inside Main Class: 内部主要类别:

Ball b;
ArrayList<Ball> balls = new ArrayList<Ball>();
int amountOfBalls;

void setup() {
    size(600, 600);
    b = new Ball();
    amountOfBalls = 4;
    for (int i = 0; i < amountOfBalls; i++) {
        balls.add(new Ball());
    }
}

void draw() {
    b.drawAll(balls);
    b.contactWall();
    b.move();
}

The four balls I drew just sit there, don't move and don't make any wacky movements, they just sit there. 我画的四个球只是坐在那里,不动,也不做任何古怪的动作,它们只是坐在那里。

As suggested by BarSahar, there is no reference of Ball in the move function call. 如BarSahar所建议,在移动功能调用中没有Ball的引用。 You can also do the below code in while loop 您还可以在while循环中执行以下代码

Ball ball = iter.next(); ball.draw(); ball.move();

But I would recommend you to move the draw All method from the Ball class and put it inside the main class. 但我建议您将Ball类的draw All方法移到主类中。 You can directly call draw All from main Class. 您可以直接从主类调用draw All。 inside draw All you can iterate using foreach loop and can call draw, move and connect`Wall. 内部绘制所有您可以使用foreach循环进行迭代,并且可以调用绘制,移动和连接Wall。

Notice that I'm your "while" loop, you only use the iterator for drawing. 注意,我是您的“ while”循环,您仅使用迭代器进行绘制。 When calling the method "move", the function doesn't have a reference for the particular ball you want to move. 当调用方法“ move”时,该函数没有要移动的特定球的引用。

I'd suggest something along the lines of: 我建议采取以下措施:

For (Ball ball : balls) {
    ball.move();
    ball.draw();
}

By the way the draw function in the main class uses the move method unnecessarily (since the ball already does that in "move"). 顺便说一句,主类中的draw函数不必要地使用了move方法(因为球已经在“ move”中完成了)。

And also I'd suggest making the "drawAll()" function a static one. 而且我建议将“ drawAll()”函数设为静态。 Since it doesn't operate on a single instance of ball "b", just as a good practice 由于它不能对球“ b”的单个实例进行操作,因此这是一个很好的做法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM