XNA 第7課 陣列
1. 宣告陣列元件(5x5=25)
Ball [] arrayBall;
int Ballcnt = 5;
2. Initialized方法加入物件
arrayBall = new Ball[Ballcnt * Ballcnt];
for (int i = 0; i < Ballcnt; i++)
for (int j = 0; j < Ballcnt; j++)
{
arrayBall[i * Ballcnt + j] = new Ball();
arrayBall[i * Ballcnt + j].Position.X = j * 60;
arrayBall[i * Ballcnt + j].Position.Y = i * 60;
}
3. Update方法加入陣列物件更新邏輯
for (int i = 0; i < Ballcnt; i++)
for (int j = 0; j < Ballcnt; j++)
UpdateArray(gameTime, arrayBall[i * Ballcnt + j]);
base.Update(gameTime);
}
void UpdateArray(GameTime gameTime, Ball BallSprite)
4. Update方法加入陣列物件更新邏輯
for (int i = 0; i < Ballcnt; i++)
for (int j = 0; j < Ballcnt; j++)
UpdateArray(gameTime, arrayBall[i * Ballcnt + j]);
void UpdateArray(GameTime gameTime, Ball BallSprite) …
5. Draw方法加入陣列物件繪圖
for (int i = 0; i < Ballcnt; i++)
for (int i = 0; i < Ballcnt; i++)
for (int j = 0; j < Ballcnt; j++)
spriteBatch.Draw(spriteTexture, arrayBall[i * Ballcnt + j].Position, Color.White); /