我很难过,需要另一双眼睛来看看这个。此代码正在工作,并突然停止工作。基本上我将一个对象添加到数组列表中。当我构建列表时,我会观察它,并且似乎每次迭代都会添加一个独特的对象。基本上会出现在屏幕上的精灵和它的x,y坐标,颜色和速度。早些时候这个工作,并且精灵将出现在屏幕上,现在它似乎复制了添加到列表中的最后一个对象,X运行循环的次数我最终得到了相同的对象。这没有任何意义...引用一个java列表总是返回最后一个元素
第一个println语句打印什么传递给构造函数。所以它打印出来如此。
球:1×123 Y:344颜色:蓝色 球:2×:3 Y 233颜色:绿色 球3×:24岁:3颜色:蓝
一切看起来大为止。然后,我居然打印列表来安慰我得到
球:1 X:24 Y:3颜色:蓝色 球:1 X:24 Y:3颜色:蓝色 球:1 X:24 Y:3颜色:蓝色
哪就在于此,我想弄清楚这是为什么发生的问题...
//When I create the List Eclipse refused to accept it until I initialized it like so...
java.util.List <Sprite> sprite = new java.util.ArrayList<Sprite>();
//yes I did import java.util.*; Eclipse still was digging it. This was working correctly despite the way i added it. I also changed this to a Vector which Eclispe was more content with with no effect.
private void GenerateSprites(){
//Random to keep it random
Random r = new Random(System.currentTimeMillis());
//variables for selecting and setting color
Color color = null;
int colorValue;
//variables for their x,y coordinates
float bX = 0;
float bY = 0;
//Create each ball set the color and generate the x,y coordinates
for (int x = 0; x < NUM_BALLS; x++){
colorValue = r.nextInt(4);
if (colorValue == 0) color = Color.BLUE;
if (colorValue == 1) color = Color.RED;
if (colorValue == 2) color = Color.YELLOW;
if (colorValue == 3) color = Color.GREEN;
bX = r.nextInt((int)(gameField.getWidth() - gameField.getWidth()/4)+SCRN_MARGIN);
bY = r.nextInt((int)(gameField.getHeight() - gameField.getHeight()/4)+SCRN_MARGIN);
//place the new ball in the gameField
//print the values being passed to the sprite constrcutor for debug purposes. The out put of this line indicates that all is well at this point.
System.out.println("Ball: " + x + " X: " + bX+ " Y: " + (bY+SCRN_MARGIN) + " Color: " + color.toString());
gSprite.add(new Sprite((float)bX, (float)bY+SCRN_MARGIN, BALL_SIZE, color));
}
//Now that the sprites are added to this list print out the list. When this line executes it shows a list of NUM_BALLS all of which have the exact sdame vlaues as the last sprite added earlier.
for (int x = 0; x < gSprite.size(); x++){
Sprite spr = gSprite.get(x);
System.out.println("Ball: " + x + " X: " + spr.getX()+ " Y: " + spr.getY() + " vX: " + spr.getvX() + " vY: " + spr.getvY() + " Color: " + spr.getColor().toString());
}
}
“此代码正在工作,并突然停止工作”...你有什么改变?代码永远不会停止*神奇地工作。 – talnicolas
如果有异常,请添加堆栈跟踪... – Crazenezz
每当您运行此代码时,什么打印到控制台? –