gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:44:21 25 4
gpt4 key购买 nike

处理 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com