我很難過,需要另一雙眼睛來看看這個。此代碼正在工作,並突然停止工作。基本上我將一個對象添加到數組列表中。當我構建列表時,我會觀察它,並且似乎每次迭代都會添加一個獨特的對象。基本上會出現在屏幕上的精靈和它的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
每當您運行此代碼時,什麼打印到控制檯? –