Java 程序在尝试执行if语句时冻结
我是一个新的处理和分配给我的任务,使苍蝇拍游戏。他们给了我们基本代码,我一直在研究这个。目前我遇到了一个问题。每当collisionDetect()发生冲突时,整个过程将冻结,没有错误消息;功能发生在鼠标按下时,我想我已经把它缩小到与swat阵列有关的范围,但这是提供的框架代码的一部分,我不完全确定是什么导致了这个问题 以下是我目前的代码:Java 程序在尝试执行if语句时冻结,java,processing,Java,Processing,我是一个新的处理和分配给我的任务,使苍蝇拍游戏。他们给了我们基本代码,我一直在研究这个。目前我遇到了一个问题。每当collisionDetect()发生冲突时,整个过程将冻结,没有错误消息;功能发生在鼠标按下时,我想我已经把它缩小到与swat阵列有关的范围,但这是提供的框架代码的一部分,我不完全确定是什么导致了这个问题 以下是我目前的代码: PImage fly,flybye,swatter,swatted; float[] fX,fY; // fly locations array floa
PImage fly,flybye,swatter,swatted;
float[] fX,fY; // fly locations array
float[] swat; // fly swatted binary boolean array, 1 = swatted, 0 = not swatted
int score=0; // increments when swatted.
void setup(){
size(800,400);
fX=new float[0];
fY=new float[0];
swat=new float[0];
// load images
fly = loadImage("fly.png");
fly.resize(50,50);
flybye = loadImage("flybye.png");
flybye.resize(50,50);
swatter = loadImage("swatter.png");
swatted = loadImage("swatted.png");
fX =append(fX, random(50,750)); //first fly - random location
fY =append(fY, random(25,375));
swat =append(swat,0); // used as a boolean and matches to each individual fly, 0 = fly not swatted, 1 = swatted.
}
void populate(){ // draw the flies in memory to the screen.
for(int i=0;i<fX.length;i++){
if(swat[i]==1){ // if swatted
// resize the fly image and place based on fx/fy array values
image(flybye, fX[0], fY[0]);
} else { // not swatted
image(fly,fX[0],fY[0]);
}
}
}
void collisionDetect(){ //collision detection - detect collision between swatter and fly
for(int i=0; i<swat.length;i++){ // bounding box detection
if(mouseX-37 > fX[0]-40 && mouseX-37 < fX[0]+40 && mouseY-30 > fY[0]-40 && mouseY-30 < fY[0]+40){ // condition should look at location of mouse and individual coordinates in fX and fY
swat[0] = 1; // swatted
image(flybye, fX[0], fY[0]);
fX =append(fX, random(50,750)); //new fly placed in random location when old fly dies.
fY =append(fY, random(50,350));
swat =append(swat,0); // new fly not swatted
score++; //increment score
}
}
}
void draw(){
background(255);
//noCursor(); //added to hide cursor for a more fluid look
populate(); // draw flys to screen.
fill(0);
// set a text size and location for the score.
if(mousePressed){ // image swap
println("Error1");
collisionDetect();
image(swatted, mouseX-37, mouseY-30); //draw swatter image to around mouse locaiton - might want to play with this to get it to look right.
println("Error");
}else{
image(swatter, mouseX-37, mouseY-30); // if not pressed then alternative image.
}
}
PImage fly,flybay,swatter,swatted;
浮动汇率[]外汇,财政年度;//飞行位置阵列
浮动[]swat;//飞拍二进制布尔数组,1=飞拍,0=未飞拍
整数分数=0;//拍打时增加。
无效设置(){
尺寸(800400);
fX=新浮动[0];
fY=新浮动[0];
swat=新浮动[0];
//加载图像
fly=loadImage(“fly.png”);
调整大小(50,50);
flybay=loadImage(“flybay.png”);
flybay.resize(50,50);
swatter=loadImage(“swatter.png”);
swatted=loadImage(“swatted.png”);
fX=append(fX,random(50750));//第一次飞行-随机位置
fY=追加(fY,随机(25375));
swat=append(swat,0);//用作布尔值并与每个苍蝇匹配,0=fly not swatted,1=swatted。
}
void populate(){//将内存中的苍蝇绘制到屏幕上。
对于(int i=0;i fY[0]-40&&mouseY-30
问题是原因,因为您正在将元素添加到容器swat
,而它正在被遍历:
for(int i=0; i<swat.length;i++) {
if (...)
...
swat = append(swat, 0);
...
}
}