为什么我的intlist值没有增加,或者为什么ellipse()函数没有响应?

问题描述 投票:0回答:1

javascript代码在处理3.5.3时不起作用,不确定原因。它应该创建圆并使其在屏幕上反弹,相反,它可以制作适当数量的圆,但它们不会移动。似乎intlist.set()无法正常工作,但我不确定为什么。帮助将不胜感激。

import javax.swing.JOptionPane;

int x = 200;
int y = 150;
int b = 50;

float slope = -1;

int numOfCircles = 10;

IntList initPosX = new IntList();
IntList initPosY = new IntList();

IntList exes = new IntList();
IntList whys = new IntList();

IntList xSpeeds = new IntList();
IntList ySpeeds = new IntList();

void setup()
{

  numOfCircles = int(JOptionPane.showInputDialog(frame, "How many circles ya want?"));
  size(800,400);
  for(int i = 0; i < numOfCircles; i++)
  {
    int toAddX = int(random(0,400));
    initPosX.append(toAddX);

    int toAddY = int(random(0,300));
    initPosY.append(toAddY);

    exes.append(0);//(int(random(-30,30)));
    whys.append(0);//(int(random(-30,30)));

    xSpeeds.append(1);
    ySpeeds.append(1);    
  }
}
void draw()
{
  background(100,100,100,255);
  for(int i = 0; i < numOfCircles; i++)
  {
    ellipse(exes.get(i) + initPosX.get(i), whys.get(i) + initPosY.get(i), 20, 20);
    exes.set(i, i + xSpeeds.get(i));
    whys.set(i, i + ySpeeds.get(i));
    if(exes.get(i) > width || exes.get(i) <= 0)
    {
      print("side wall hit");
      xSpeeds.set(i, i*= slope);
    }
    if(whys.get(i) > height || whys.get(i) <= 0)
    {
      print("roof hit");
      ySpeeds.set(i, i*= slope);
    }
  }
}
java processing ellipse
1个回答
0
投票

问题出在那几行:

exes.set(i, i + xSpeeds.get(i));
whys.set(i, i + ySpeeds.get(i));

您想在此处执行的操作是将速度添加到索引i处的exes / whys当前值。但是,您实际要做的是将它们设置为索引+速度。由于索引永远不会改变,所以位置也不会改变。

要解决此问题,请替换为:

exes.set(i, exes.get(i) + xSpeeds.get(i));
whys.set(i, exes.get(i) + ySpeeds.get(i));
© www.soinside.com 2019 - 2024. All rights reserved.