Question

Im trying to build a break out game using C# and the XNA framework. I've placed the brick into a seperate class and created an array for 10 bricks to load. However, when I run the game, no bricks are actually appearing and I can't seem to find the error as Visual Studio isn't flagging anything.

Brick class:

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);
        }
    }
}

Main program:

/// <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();
    }
}
Was it helpful?

Solution

You never Initialise the bricks object.
You need to call bricks.Initialise(); function somewhere (preferably in the initialise function of the Breakout class).

I would also recommend you to use a bit cleaner code standard.
Class names usually start with upper case, ie: Bricks instead of bricks, in your case, the class and the instance of the class have the same name (bricks) which could create confusion and even should create a compiler error (I think?).
(The 'NewTexture' variable in the constructor of the brick class also suffer from bad naming, should probably rather be named newTexture, especially due to the fact that the Rect is called newRectangle, - but this is not a code-review site, so ill leave that issue for now!).

OTHER TIPS

Try passing null into the sourceRectangle parameter of spriteBatch.Draw in your bricks class. I think the rectangle you're passing in is no good.

spritebatch.Draw(brickimg, new Vector2(brickXPos[i], brickYPos[i]), null, Color.White);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top