-2

I am trying to make Pong in XNA/C# using a class for the Paddle and Ball

Game1.cs:

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 Pong
{

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

        Paddle Paddle1 = new Paddle();

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

        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

        }

        protected override void Update(GameTime gameTime)
        {

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

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

            Paddle1.Draw();
            base.Draw(gameTime);
        }
    }
}

Paddle.cs:

namespace Pong
{
    class Paddle
    {
        SpriteBatch spriteBatch;
        ContentManager Content;

        Texture2D paddle1;
        Texture2D paddle2;

        Vector2 Paddle1;
        Vector2 Paddle2;

        public void LoadContent()
        {
            paddle1 = Content.Load<Texture2D>("pongpaddle1");

            Paddle1 = new Vector2();
            Paddle1.X = 50;
            Paddle1.Y = 50;
        }

        public void Draw()
        {
            spriteBatch.Begin(); //Causes NullReferenceException was unhandled, Object reference not set to an instance of an object.
            spriteBatch.Draw(paddle1, Paddle1, Color.White);
            spriteBatch.End();
        }
    }
}

I don't have anything in the Ball class yet, but it will use similar methods to Paddle.cs

Every time I've ran the code, I keep getting a System.StackOverFlow exception whenever it hits this line of code in Game1.cs:

Paddle Paddle1 = new Paddle();

How do I fix this? I don't see how it's run out of memory already.

EDIT: Updated code.

vyegorov
  • 21,787
  • 7
  • 59
  • 73
  • You usually fix SO errors by letting your program run inside a debugger, let it break upon exceptions and then inspect the call stack carefully, then think hard about why the SO occurs and how to fix this. – Uwe Keim May 05 '12 at 22:08
  • Thats what I'm using is Visual C# 2010 Express, and it's giving me: An unhandled exception of type 'System.StackOverflowException' occurred in Pong.exe – Bill Fountaine May 05 '12 at 22:10
  • 1
    Posting your code for the `Paddle1` property directly in question would help. – ChrisF May 05 '12 at 22:10
  • I suspect you are calling the setter from the setter in your property code, but I'm not going to look in paste bin to check. – ChrisF May 05 '12 at 22:11
  • Set current method = `Paddle` constructor. For Each method In current method: { If current method is Paddle constructor Then { Found possible cause } Else { Repeat for method } } – Ry- May 05 '12 at 22:11
  • 2
    Your load content is recursively loading itself. – Andrew Barrett May 05 '12 at 22:11
  • @ ChrisF, the code I posted is the entirety of the code. – Bill Fountaine May 05 '12 at 22:14
  • @BillFountaine remove this line from game1.cs `Paddle1.LoadContent();` you are telling it to load in both the parent and child class. That is recursive calling = infinite loop = SO error – Lyuben Todorov May 05 '12 at 22:24

2 Answers2

0
    public class Game1 : Microsoft.Xna.Framework.Game
    {
         Paddle Paddle1 = new Paddle();
         Paddle Paddle2 = new Paddle();
    ...
     protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            Paddle1.LoadContent();
        }
    ...
    }

    class Paddle : Game1
    {
    ...
        protected override void LoadContent()
        {
            Paddle1 = new Vector2();
            Paddle1.X = 50;
            Paddle1.Y = 50;
            base.LoadContent();
        }
    ...
    }

Two big problems here, there is a recursive LoadContent call. Not to mention your paddles have paddles which have paddles... Why is your paddle inheriting from Game1? It almost definitely shouldn't be.

Also your paddle instances instantiate other paddle instances, so you're in a loop of instantiating other paddle classes. It seems like you might want to take a step back and just get used to some basic code first? For what it's worth, I wrote pong in xna for fun a few years back, it's a bit messy, but it might give you some starting help.

Here is an example of a paddle class based off the DrawableGameComponent class (drawn in primatives so it's a bit verbose):

public class Paddle : DrawableGameComponent
{
    private readonly VertexPositionColor[] _vertices = new VertexPositionColor[6];
    private readonly float _width;
    private readonly float _height;

    private IndexBuffer _indexbuffer;
    private VertexBuffer _vertexbuffer;

    public Vector3 Position { get; set; }
    public Vector3 Direction { get; set; }
    public float Speed { get; set; }

    public Paddle(Game game, float width, float height)
        : base(game)
    {
        _width = width;
        _height = height;
    }

    protected override void LoadContent()
    {
        base.LoadContent();

        _vertices[0].Position = new Vector3(0, 0, 0);
        _vertices[0].Color = Color.Red;

        _vertices[1].Position = new Vector3(_width, _height, 0);
        _vertices[1].Color = Color.Green;

        _vertices[2].Position = new Vector3(0, _height, 0);
        _vertices[2].Color = Color.Blue;

        _vertices[3].Position = new Vector3(_width, 0, 0);
        _vertices[3].Color = Color.Green;

        _vertexbuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionColor), _vertices.Length, BufferUsage.WriteOnly);
        _vertexbuffer.SetData(_vertices);

        var indices = new short[6];
        indices[0] = 0;
        indices[1] = 1;
        indices[2] = 2;
        indices[3] = 0;
        indices[4] = 3;
        indices[5] = 1;

        _indexbuffer = new IndexBuffer(GraphicsDevice, typeof(short), 6, BufferUsage.WriteOnly);
        _indexbuffer.SetData(indices);
    }

    public BoundingBox GetBoundingBox()
    {
        return new BoundingBox(Position, Position + new Vector3(_width, _height, 0));
    }

    public override void Draw(GameTime gameTime)
    {
        base.Draw(gameTime);

        GraphicsDevice.SetVertexBuffer(_vertexbuffer);
        GraphicsDevice.Indices = _indexbuffer;
        GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4, 0, 2);
    }
}
Andrew Barrett
  • 19,721
  • 4
  • 47
  • 52
0

What's happening here is that Paddle inherits Game1. Game1 creates new Paddles:

Paddle Paddle1 = new Paddle();
Paddle Paddle2 = new Paddle();

Those Paddles are Games that need to initialize their own set of Paddles. Infinite recursion! I'm not sure how XNA works, but if that's how the inheritance should be, just move your initializations to Initialize():

// TODO: Add your initialization logic here

base.Initialize();
this.Paddle1 = new Paddle();
this.Paddle2 = new Paddle();

I kind of doubt that a game object should inherit from the game itself, though. That would seem like a rather poor design decision.

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • I'm still learning how to program correctly. It's a pain to learn when there are a million tutorials/books out there and none seem to help for squat :/ – Bill Fountaine May 05 '12 at 22:18