0

現在、学校の評価用にゲームのリメイクを作っているので、間違っていたらすみません。foreach ループを作成して、敵の飛行機の数を画面に描画しようとしました。アップデートゲームタイムで1ループしました。エラーは「NullReferenceException was Unhandled」で、その下には「オブジェクト参照がオブジェクトのインスタンスに設定されていません」があります。この同じコードが、クラスで作成した別の「実践的な」ゲームで機能したため、何が間違っているのかわかりません。

        foreach (EnemyPlane ball in enemyplaneObjects)
        {
            ball.Update();
        }

ドローアップデートでもう1つ。

        foreach (EnemyPlane ball in enemyplaneObjects)
        {
            ball.Update();
        }

考慮すべきもう1つのことは、両方の「ボール」参照の値がnullであり、配列またはスプライトバッチで宣言されていないことです。

これがスプライトバッチです。

    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;



    Texture2D truckTexture;
    Vector2 truckPosition;

    Texture2D planeTexture;
    Vector2 planePosition;


    Texture2D backgroundTexture;
    Vector2 backgroundPosition;

    Texture2D enemyplaneTexture;
    Vector2 enemyplanePosition;
    int enemyplaneCount = 5;



    Texture2D personTexture;
    Vector2 personPosition;
    Parachute personObject;

    Vector2 spriteVelocity = new Vector2(0.5f, 0f);

    Random rand = new Random();

   EnemyPlane[] enemyplaneObjects;

これが配列です。

        enemyplaneObjects = new EnemyPlane[enemyplaneCount];

        for (int index = 0 ; index < enemyplaneCount; index++)
        {
            byte r = (byte)rand.Next(64, 256); //Red Value
            byte g = (byte)rand.Next(64, 256); //Green Value
            byte b = (byte)rand.Next(64, 256); //Blue Value
            byte a = (byte)rand.Next(64, 256); //Alpha Value
            Color tempColor = new Color(r, g, b, a);
            enemyplaneObjects[0] = new EnemyPlane(EnemyPlane.Texture, new Vector2(rand.Next(2, 100), rand.Next(2, 100)), new Vector2(rand.Next(-2, 20), rand.Next(-2, 20)), tempColor);

        }

前もって感謝します。

4

1 に答える 1

2

配列を初期化するには、反復する変数を使用する必要があります。今のところ、配列の最初のエントリのみを初期化しています。後で foreach でそれらをループすると、Update() の呼び出しは最初のエントリでのみ機能し、次のエントリで例外がスローされます。

enemyplaneObjects[index] = new EnemyPlane(EnemyPlane.Texture, new Vector2(rand.Next(2, 100), rand.Next(2, 100)), new Vector2(rand.Next(-2, 20), rand.Next(-2, 20)), tempColor);
于 2013-05-31T03:34:39.577 に答える