2014-02-19 150 views
0

我試圖用C#和XNA框架構建一個突破遊戲。我已經將磚塊放入一個單獨的類中,並創建了一個可載入10塊磚的數組。但是,當我運行遊戲時,實際上沒有出現磚塊,並且我似乎無法找到該錯誤,因爲Visual Studio沒有標記任何東西。內容不從類加載

磚類:

class bricks 
{ 
    Texture2D brickimg; 
    Rectangle rectangle; 
    int[] brickXPos = new int[50]; 
    int[] brickYPos = new int[50]; 

    public bricks(Texture2D NewTexture, Rectangle newRectangle) 
    { 
     brickimg = NewTexture; 
     rectangle = newRectangle; 
    } 

    public void Initialize() 
    { 
     for (int i = 0; i < 50; i++) 
     { 
      brickXPos[i] = 300 + i * 60; 
      brickYPos[i] = 100; 
     } 
    } 

    public void Update() 
    { 
    } 

    public void Draw(SpriteBatch spritebatch) 
    { 
     for (int i = 0; i < 50; i++) 
     { 
      spritebatch.Draw(brickimg, new Vector2(brickXPos[i], brickYPos[i]), rectangle, Color.White); 
     } 
    } 
} 

主程序:

/// <summary> 
/// This is the main type for your game 
/// </summary> 
public class Breakout : Microsoft.Xna.Framework.Game 
{ 
    GraphicsDeviceManager graphics; 
    SpriteBatch spriteBatch; 
    Texture2D BackgroundImg; 
    bricks bricks; 

    public Breakout() 
    { 
     graphics = new GraphicsDeviceManager(this); 

     graphics.PreferredBackBufferWidth = 1024; 
     graphics.PreferredBackBufferHeight = 768; 
     graphics.ApplyChanges(); 
     Content.RootDirectory = "Content"; 
    } 

    /// <summary> 
    /// Allows the game to perform any initialization it needs to before starting to run. 
    /// This is where it can query for any required services and load any non-graphic 
    /// related content. Calling base.Initialize will enumerate through any components 
    /// and initialize them as well. 
    /// </summary> 
    protected override void Initialize() 
    { 
     // TODO: Add your initialization logic here 

     base.Initialize(); 
    } 

    /// <summary> 
    /// LoadContent will be called once per game and is the place to load 
    /// all of your content. 
    /// </summary> 
    protected override void LoadContent() 
    { 
     // Create a new SpriteBatch, which can be used to draw textures. 
     spriteBatch = new SpriteBatch(GraphicsDevice); 
     BackgroundImg = Content.Load<Texture2D>("starfield"); 
     bricks = new bricks(Content.Load<Texture2D>("redbrick"),new Rectangle()); 
     // TODO: use this.Content to load your game content here 
    } 

    /// <summary> 
    /// UnloadContent will be called once per game and is the place to unload 
    /// all content. 
    /// </summary> 
    protected override void UnloadContent() 
    { 
     // TODO: Unload any non ContentManager content here 
    } 

    /// <summary> 
    /// Allows the game to run logic such as updating the world, 
    /// checking for collisions, gathering input, and playing audio. 
    /// </summary> 
    /// <param name="gameTime">Provides a snapshot of timing values.</param> 
    protected override void Update(GameTime gameTime) 
    { 
     // Allows the game to exit 
     if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 
      this.Exit(); 

     // TODO: Add your update logic here 

     base.Update(gameTime); 
    } 

    /// <summary> 
    /// This is called when the game should draw itself. 
    /// </summary> 
    /// <param name="gameTime">Provides a snapshot of timing values.</param> 
    protected override void Draw(GameTime gameTime) 
    { 
     GraphicsDevice.Clear(Color.CornflowerBlue); 

     // TODO: Add your drawing code here 

     base.Draw(gameTime); 
     spriteBatch.Begin(); 
      spriteBatch.Draw(BackgroundImg, Vector2.Zero, Color.White); 
      bricks.Draw(spriteBatch); 
      spriteBatch.End(); 
    } 
} 
+2

請將您的'bricks'類名更改爲'Bricks'。按標準編碼。 –

回答

0

你從來沒有初始化的對象磚。
您需要在某處調用bricks.Initialise();函數(最好在Breakout類的初始化函數中)。

我也建議你使用一點清潔的代碼標準。
類名通常以大寫字母開頭,即:Bricks而不是bricks,在您的情況下,類和類的實例具有相同的名稱(bricks),這可能會造成混淆,甚至應該創建編譯器錯誤(我認爲?)。
brick類的構造函數中的'NewTexture'變量也會受到錯誤命名的影響,或許應該命名爲newTexture,特別是由於Rect被稱爲newRectangle的事實 - 但這不是代碼審閱站點,所以生病了,現在就離開這個問題!)。

+0

added bricks.Initialise();放入inialise方法中的breakout.cs文件中,並將類名更改爲大寫。然而,沒有磚仍然顯示 – Bobby

+1

檢查fooser的答案,它包含一些我沒有注意到的信息:你傳遞的矩形可能是用'(0,0,0,0)'初始化的(因爲你使用默認的構造函數,no args),那不太好。你可能只想在那裏傳遞null。 – Jite

+0

是的!即時與你現在,我記得把他們留在0,因爲我不確定的大小。感謝你們對我們的幫助 – Bobby

0

嘗試將null轉換成您磚類中的spriteBatch.Draw的參數sourceRectangle。我認爲你傳遞的矩形是不好的。

spritebatch.Draw(brickimg, new Vector2(brickXPos[i], brickYPos[i]), null, Color.White); 
+0

偉大的他們現在正在繪製完美!所以我怎麼會去圍繞每個矩形繪製一個矩形來進行碰撞檢測? – Bobby

+0

我可能不得不用一些實際的代碼來回答你,但是對於一個邊界框來說,一個好的起點可能是:具有原點的矩形(brickXPos [i],brickYPos [i])和高度爲brickImg。高度和寬度brickImg.Width。根據你需要的複雜程度和精確度,如果你在磚塊上添加比例和/或旋轉,這些事情會變得棘手。 – fooser

+0

爲什麼從此答案中刪除「接受的答案」? :( – fooser