使用基类继承时构造函数出错

时间:2013-09-16 21:28:19

标签: c# xna-4.0 base

嘿,我是c#和XNA的新手。我的目标是从一个Sprite类继承并生成诸如pongSprite,paddleSprite等类......我的构造函数出错了。我扩展了sprite类,并将Sprite类中的变量和对象放入:base()

这是我的代码:

**

  • Sprite.cs

**

namespace SimplePong
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Sprite : DrawableGameComponent
    {
        protected string id;
        protected Texture2D texture;
        //bounding box
        //protected Rectangle sourceRectangle;
        protected Rectangle destinationRectangle;
        protected Color color;

        protected Main game;
        private Sprite pongBall;
        public Sprite(Main game, string id, Texture2D texture,
            Rectangle destinationRectangle, Color color)
            : base(game)
        {
            this.game = game;
            this.id = id;
            this.texture = texture;
            this.destinationRectangle = destinationRectangle;
            this.color = color;  
        }
        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here
            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            game.spriteBatch.Begin();
            game.spriteBatch.Draw(texture, destinationRectangle, color);
            game.spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}

**

  • ballSprite.cs

**

using Microsoft.Xna.Framework;

namespace SimplePong
{
    public class BallSprite : Sprite
    {
        // public Main game;
        public Sprite pongBall;
        public BallSprite()
            : base(Main game, string id, Texture2D texture,
            Rectangle destinationRectangle, Color color)
        {

        }
        public override void Initialize()
        {
            base.Initialize();
        }
        public override void Update(GameTime gameTime)
        {
            destinationRectangle.X += 2;
            destinationRectangle.Y += 2;

            base.Update(gameTime);
        }

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

1 个答案:

答案 0 :(得分:1)

您继承的类的构造函数的语法是错误的。我怀疑你想要:

public BallSprite(Main game, string id, Texture2D texture,
    Rectangle destinationRectangle, Color color)
    : base(game, id, texture,  destinationRectangle, color)
{

}

如果你想要一个无参数构造函数,那么你将不得不自己想出参数的值(或者不要调用基础构造函数)。