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

标签 java processing ellipse

处理 3.5.3 中的 JavaScript 代码无法正常工作,不知道为什么。它应该创建圆圈并让它们在屏幕上弹跳,但它创建了适量的圆圈,但它们不移动。似乎 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);
    }
  }
}

最佳答案

问题出在这些行:

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, whys.get(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);
}

不检测实际位置的碰撞,因为那将是位置(exes,whys)+ initPos 的,所以它应该是

if (exes.get(i) + initPosX.get(i) > width || exes.get(i) + initPosX.get(i) <= 0)
{
   //the code
}
if (whys.get(i) + initPosY.get(i) > height ||  whys.get(i) + initPosY.get(i) <= 0)
{
   //the code
}

但是,如果您现在启动它,您会收到错误消息。那是因为你变成了消极的东西。而不是 i*=lope 只需使用 int(i *lope) (因为 int * float 返回 float ,您必须使用 int() 将结果转换为 int ) .

此外,您实际上并不需要索引,而是需要索引处的当前值:

xSpeeds.set(i, int(xSpeeds.get(i) * slope); //the same for ySpeeds

关于java - 为什么我的 intlist 值没有增加,或者为什么 ellipse() 函数没有响应它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58717005/

相关文章:

java - 输入验证检查输入是否为 double

java - 如何将项目文件夹从 IDEA 推送到 Github 存储库

java - Spring集成中的Session存储以及处理多个http出站网关

javascript - 为什么我的代码直接进入结束条件?

java - 如何从另一个线程更改处理中的背景和变量

r - ggplot2 & stat_ellipse : Draw ellipses around multiple groups of points

javascript - 贝塞尔曲线在 HTML5 Canvas 中绘制拉伸(stretch)椭圆

java - 使用 java8 合并对象

arrays - 处理中的 SplitTokens 问题

python - Altair - 在多面图中绘制圆、椭圆、注释