私は困惑しており、これを見るためにもう一組の目が必要です。このコードは動作していて、突然動作を停止しました。基本的に私はarraylistにオブジェクトを追加しています。私がリストを作成しているとき、それを見ると、繰り返しごとに一意のオブジェクトを追加するようです。基本的に画面に表示されるスプライトとそのx、y座標、色、および速度。以前はこれが機能し、スプライトが画面に散在して表示されるようになりましたが、リストに最後に追加されたオブジェクトが重複しているように見えます。それは意味をなさない...Javaのリストを参照すると常に最後の要素が返されます
最初のprintlnステートメントは、コンストラクタに渡されるものを出力します。それで、それはそういう形で印刷されます。
ボール:1のx:123 Y:344色:ブルー ボール:2×:3 yの233色:グリーン ボール×3:24 Y: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
このコードを実行するたびにコンソールには何が出力されますか? –