-2

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

游戏1.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);
        }
    }
}

桨.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();

我该如何解决?我不明白它是如何内存不足的。

编辑:更新的代码。

4

2 回答 2

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 中写了 pong ,它有点乱,但它可能会给你一些开始的帮助。

这是一个基于 DrawableGameComponent 类的 paddle 类的示例(以原始形式绘制,因此有点冗长):

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);
    }
}
于 2012-05-05T22:13:32.873 回答
0

这里发生的是Paddle继承Game1. Game1创建new Paddles:

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

那些Paddles 是Game需要初始化自己的 s 集合的Paddles。无限递归!我不确定 XNA 是如何工作的,但如果继承应该是这样,只需将您的初始化移动到Initialize()

// TODO: Add your initialization logic here

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

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

于 2012-05-05T22:14:32.623 回答