Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 处理2.1.1-一次在屏幕上显示多个项目?_Java_Animation_Processing - Fatal编程技术网

Java 处理2.1.1-一次在屏幕上显示多个项目?

Java 处理2.1.1-一次在屏幕上显示多个项目?,java,animation,processing,Java,Animation,Processing,我对编程比较陌生,我正在尝试创建一个程序,创建一个紫色的球,我点击它向右移动,直到它离开屏幕,在那里我可以一次在屏幕上有无限的球。我已经制作了一个程序来实现这一点,但我一次只能在屏幕上显示一个,如果我再次单击,第一个球就会消失,并被一个新球取代。哦,当我再次单击时,球不是从光标所在的位置开始,而是从X轴上最后一个球所在的位置开始。 救命啊 代码如下: int moveX, moveY; void setup() { background(255); moveY = 200; mov

我对编程比较陌生,我正在尝试创建一个程序,创建一个紫色的球,我点击它向右移动,直到它离开屏幕,在那里我可以一次在屏幕上有无限的球。我已经制作了一个程序来实现这一点,但我一次只能在屏幕上显示一个,如果我再次单击,第一个球就会消失,并被一个新球取代。哦,当我再次单击时,球不是从光标所在的位置开始,而是从X轴上最后一个球所在的位置开始。 救命啊

代码如下:

int moveX, moveY;

void setup() {
  background(255);
  moveY = 200;
  moveX = 0;
  size(500,400);
}

void mouseClicked() {
moveY = mouseY;
  moveX++;

}

void draw() {
  if (moveX >= 1){
    background(255);
    fill(255, 0, 255);
    ellipse(moveX, moveY, 40, 40);
    moveX++;
    }
}

正如donfuxx所建议的,给每个球它自己的坐标。 一种方法是使用数组存储多个值(坐标)

为此,您需要熟悉for循环和数组。 一开始它们可能看起来很吓人,但一旦你掌握了窍门,它们就很容易了。 只要你能想到需要重复的情况,你就可以使用for循环让你的生活更轻松

For循环具有以下语法:

for keyword (3 elements: a start point,an end point(condition) and an increment,(separated by the ; character)
假设您希望一次从a(0)移动到b(10):

for(int currentPos = 0 ; currentPos < 10; currentPos++){
  println("step: " + currentPos);
}
这在遍历所有类型的数据(场景中球的坐标等)时非常有用

如何组织数据?将其放置在列表或数组中。 数组包含相同类型的元素并具有设置的长度。 声明数组的语法如下所示:

ObjectType[] nameOfArray;
您可以初始化空数组:

int[] fiveNumbers = new int[5];//new keyword then the data type and length in sq.brackets
或者可以使用以下值初始化数组:

String[] words = {"ini","mini","miny","moe"};
使用方括号和要访问的列表中对象的索引访问数组中的元素。数组具有长度属性,因此可以方便地计算对象

background(255);
String[] words = {"ini","mini","miny","moe"};
for(int i = 0 ; i < words.length; i++){
   fill(map(i,0,words.length, 0,255));
   text(words[i],10,10*(i+1));
}
background(255);
String[]words={“ini”、“mini”、“miny”、“moe”};
for(int i=0;i
现在回到你原来的问题。 以下是用于循环和数组的代码:

int ballSize = 40;
int maxBalls = 100;//maximum number of balls on screen
int screenBalls = 0;//number of balls to update
int[] ballsX = new int[maxBalls];//initialize an empty list/array of x coordinates
int[] ballsY = new int[maxBalls];//...and y coordinates
void setup() {
  size(500, 400);
  fill(255, 0, 255);
}
void mouseClicked() {
  if (screenBalls < maxBalls) {//if still have room in our arrays for new ball coordinates
    ballsX[screenBalls] = mouseX;//add the current mouse coordinates(x,y)
    ballsY[screenBalls] = mouseY;//to the coordinate arrays at the current ball index
    screenBalls++;//increment the ball index
  }
}

void draw() {
  println(screenBalls);
  background(255);
  for (int i = 0 ; i < screenBalls; i++) {//start counting from 0 to how many balls are on screen
    ballsX[i]++;//increment the x of each ball
    if(ballsX[i]-ballSize/2 > width) ballsX[i] = -ballSize/2;//if a ball goes off screen on the right, place it back on screen on the left
    ellipse(ballsX[i], ballsY[i], ballSize, ballSize);//display each ball
  }
}
int ballSize=40;
int maxBalls=100//屏幕上的最大球数
int屏幕球=0//要更新的球数
int[]ballsX=新int[maxBalls]//初始化x坐标的空列表/数组
int[]ballsY=新int[maxBalls];/。。。和y坐标
无效设置(){
尺寸(500400);
填充(255,0,255);
}
void mouseClicked(){
if(screenball宽度)ballsX[i]=-ballSize/2;//如果一个球离开右侧屏幕,则将其放回左侧屏幕
椭圆(ballsX[i],ballsY[i],ballSize,ballSize);//显示每个球
}
}

解决这个问题有多种方法。数组具有固定大小。如果您不想受到限制,可以使用ArrayList(一种可变大小的数组)。稍后,您可能希望了解如何制作一个可以更新和绘制自身的图形。玩得开心

你的问题很不清楚,你需要更清楚你的问题是什么。据我猜测,你只保留了一份球坐标的副本。那么,如何创建其他球?你需要设置所有球的坐标。这些函数在你的程序中是如何工作的?给每个球指定它自己的坐标
background(255);
String[] words = {"ini","mini","miny","moe"};
for(int i = 0 ; i < words.length; i++){
   fill(map(i,0,words.length, 0,255));
   text(words[i],10,10*(i+1));
}
int ballSize = 40;
int maxBalls = 100;//maximum number of balls on screen
int screenBalls = 0;//number of balls to update
int[] ballsX = new int[maxBalls];//initialize an empty list/array of x coordinates
int[] ballsY = new int[maxBalls];//...and y coordinates
void setup() {
  size(500, 400);
  fill(255, 0, 255);
}
void mouseClicked() {
  if (screenBalls < maxBalls) {//if still have room in our arrays for new ball coordinates
    ballsX[screenBalls] = mouseX;//add the current mouse coordinates(x,y)
    ballsY[screenBalls] = mouseY;//to the coordinate arrays at the current ball index
    screenBalls++;//increment the ball index
  }
}

void draw() {
  println(screenBalls);
  background(255);
  for (int i = 0 ; i < screenBalls; i++) {//start counting from 0 to how many balls are on screen
    ballsX[i]++;//increment the x of each ball
    if(ballsX[i]-ballSize/2 > width) ballsX[i] = -ballSize/2;//if a ball goes off screen on the right, place it back on screen on the left
    ellipse(ballsX[i], ballsY[i], ballSize, ballSize);//display each ball
  }
}