如何在我的Pong代码中修复此System.StackOverFlow异常?

时间:2012-05-05 22:06:30

标签: c# xna-4.0

我正在尝试使用Paddle and Ball的类来在XNA / C#中制作Pong

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

我在Ball类中没有任何东西,但是它将使用与Paddle.cs类似的方法

每次运行代码时,每当遇到Game1.cs中的这行代码时,我都会收到System.StackOverFlow异常:

Paddle Paddle1 = new Paddle();

我该如何解决这个问题?我不知道它已经耗尽了内存。

编辑:更新了代码。

2 个答案:

答案 0 :(得分: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();
        }
    ...
    }

这里有两个大问题,就是有一个递归的LoadContent调用。更不用说你的桨有桨有桨 - 为什么你的桨从Game1继承?它几乎绝对不应该

您的paddle实例实例化其他paddle实例,因此您处于实例化其他paddle类的循环中。 看起来你可能想退后一步,先熟悉一些基本代码?对于它的价值,几年前我在xna中wrote pong获得乐趣,它有点乱,但它可能会给你一些起步帮助。

这是一个基于DrawableGameComponent类的paddle类的示例(在primatives中绘制,因此它有点冗长):

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

答案 1 :(得分:0)

这里发生的事情是Paddle继承Game1Game1创建new Paddle s:

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

那些PaddleGame,需要初始化自己的一组Paddle。无限递归!我不确定XNA是如何工作的,但如果这就是继承应该如何,只需将初始化移到Initialize()

// TODO: Add your initialization logic here

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

我有点怀疑游戏对象应该继承游戏本身。这似乎是一个相当糟糕的设计决定。