2012-10-30 45 views
1
using System; 
using System.Collections.Generic; 
using System.Linq; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Audio; 
using Microsoft.Xna.Framework.Content; 
using Microsoft.Xna.Framework.GamerServices; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework.Input; 
using Microsoft.Xna.Framework.Media; 

namespace WindowsGame3 
{ 
    public class Game1 : Microsoft.Xna.Framework.Game 
{ 
    GraphicsDeviceManager graphics; 
    SpriteBatch spriteBatch; 

    Texture2D ship, bullet, alien; 

    Texture2D[] aliens = new Texture2D[20]; 
    Texture2D[] bullets = new Texture2D[10]; 

    Vector2 shipPos = new Vector2(200, 540); 
    Vector2 alienPos = new Vector2(200, 400); 
    Vector2 bulletPos = new Vector2(200, 450); 

    Vector2[] aliensPos = new Vector2[20]; 
    Vector2[] bulletsPos = new Vector2[10]; 

    int alienCount = 0; 
    int bulletCount = 0; 

    KeyboardState state = Keyboard.GetState(); 

    public Game1() 
    { 
     graphics = new GraphicsDeviceManager(this); 
     Content.RootDirectory = "Content"; 

     graphics.PreferredBackBufferWidth = 400; 
     graphics.PreferredBackBufferHeight = 600; 
    } 

    protected override void Initialize() 
    { 
     base.Initialize(); 
    } 

    protected override void LoadContent() 
    { 
     spriteBatch = new SpriteBatch(GraphicsDevice); 

     alien = Content.Load<Texture2D>("alien"); 
     ship = Content.Load<Texture2D>("ship"); 

     for (int x = 0; x < 5; x++) 
     { 
      for (int y = 0; y < 4; y++) 
      { 
       aliens[alienCount] = Content.Load<Texture2D>("alien"); 
       aliensPos[alienCount].X = 100 + (x * 40); 
       aliensPos[alienCount].Y = 20 + (y * 40); 
       alienCount++; 
      } 
     } 
    } 

    protected override void UnloadContent() 
    { 

    } 

    protected override void Update(GameTime gameTime) 
    { 
     if (state.IsKeyDown(Keys.Right) && shipPos.X < 360) 
     { 
      shipPos.X += 5; 
     } 

     if (state.IsKeyDown(Keys.Left) && shipPos.X > 0) 
     { 
      shipPos.X -= 5; 
     } 

     if (state.IsKeyDown(Keys.Space)) 
     { 
       bullets[bulletCount] = Content.Load<Texture2D>("bullet"); 
       bulletsPos[bulletCount].X = shipPos.X; 
       bulletsPos[bulletCount].Y = shipPos.Y; 
       bulletCount++; 
     } 

     base.Update(gameTime); 
    } 

    protected override void Draw(GameTime gameTime) 
    { 
     GraphicsDevice.Clear(Color.Black); 

     spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend); 

     spriteBatch.Draw(ship, shipPos, Color.White); 
     spriteBatch.Draw(alien, alienPos, Color.White); 

     for (int alienCount = 0; alienCount < 20; alienCount++) 
     { 
      spriteBatch.Draw(aliens[alienCount], aliensPos[alienCount], Color.White); 
     } 

     for (int bulletCount = 0; bulletCount < 10; bulletCount++) 
     { 
      spriteBatch.Draw(bullets[bulletCount], bulletsPos[bulletCount], Color.White); 
     } 

     spriteBatch.End(); 
     base.Draw(gameTime); 
    } 
} 
} 

我想在船隻位置創建一個子彈並將該子彈存儲在一個數組中,但在繪製方法中,當我從數組中繪製子彈時出現錯誤消息此方法對於這個參數不接受null。 參數名稱:紋理xna方法不接受null

+0

你傳遞一個參數爲空。這是不允許的。 –

+1

看...有...在代碼中有很多錯誤,我不知道從哪裏開始。我只能希望它是一個編輯過的代碼片段。 – LightStriker

+0

我可以看到你對XNA和編程很新。雖然能夠製作自己的視頻遊戲的魅力非常令人興奮,但我認真地建議您在處理這類問題之前,先學習一些關於C#的基本教程,並學習一些編程原則,或者您將頭靠在牆上數週。 –

回答

0

這是因爲您在製作它們之前試圖繪製子彈。解決方法是在製作它們之前不要畫子彈。

有任何數量的方式來做到這一點,但這裏的最快和最骯髒的方式:

變化

for (int bulletCount = 0; bulletCount < 10; bulletCount++) 
    { 
     spriteBatch.Draw(bullets[bulletCount], bulletsPos[bulletCount], Color.White); 
    } 

for (int bulletCount = 0; bulletCount < 10; bulletCount++) 
    { 
     if(bullets[bulletCount] != null) 
      spriteBatch.Draw(bullets[bulletCount], bulletsPos[bulletCount], Color.White); 
    } 
+0

這工作,但現在我的船不會移動大聲笑 –

+0

我教自己,所以當然它不會是完美的。但隨時給我提示,我可以做得更好 –

3

在XNA這是很好的加載所有資源在你需要它們之前,那就是你有什麼LoadContent()方法。您應該在其中加載子彈紋理,並在必要時在Update()中使用它。

即使您想繪製多次,您每個圖像只需要一個Texture2D對象。 您不需要完全相同圖像的Texture2D數組。

所以,你的例子:

protected override void LoadContent() 
{ 
    spriteBatch = new SpriteBatch(GraphicsDevice); 

    alien = Content.Load<Texture2D>("alien"); //only one texture needed 
    ship = Content.Load<Texture2D>("ship"); 
    bullet = Content.Load<Texture2D>("bullet"); //only one texture needed 

    //set initial positions as before 
    for (int x = 0; x < 5; x++) 
    { 
     for (int y = 0; y < 4; y++) 
     { 
      aliensPos[alienCount].X = 100 + (x * 40); 
      aliensPos[alienCount].Y = 20 + (y * 40); 
      alienCount++; 
     } 
    } 
} 
當你想新的外來或子彈

,您只需設置自己的位置,因爲圖像已經被加載:

if (state.IsKeyDown(Keys.Space)) 
{ 
     bulletsPos[bulletCount].X = shipPos.X; 
     bulletsPos[bulletCount].Y = shipPos.Y; 
     bulletCount++; 
} 

那麼,當你有所有的紋理加載和準備,你可以畫出它:

protected override void Draw(GameTime gameTime) 
{ 
    GraphicsDevice.Clear(Color.Black); 

    spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend); 

    spriteBatch.Draw(ship, shipPos, Color.White); 

    for (int alienCount = 0; alienCount < 20; alienCount++) 
    { 
     //draw alien texture for every aliensPos 
     spriteBatch.Draw(alien, aliensPos[alienCount], Color.White); 
    } 

    for (int bulletCount = 0; bulletCount < 10; bulletCount++) 
    { 
     //draw bullet texture for every bulletsPos 
     spriteBatch.Draw(bullet, bulletsPos[bulletCount], Color.White); 
    } 

    spriteBatch.End(); 
    base.Draw(gameTime); 
} 

最後,爲了讓你的船移動,你應該每幀讀取鍵盤。您只能調用一次Keyboard.GetState()。當你閱讀它時,它會返回當前按下的按鍵,所以你需要每一幀刷新它。

你應該添加到您的更新的非常begining()方法:

state = Keyboard.GetState();