-2

Hey im new to c# and XNA. My goal is to inherit from a Sprite class and make classes such as pongSprite, paddleSprite etc... Im getting an error on my constructor. I have extended the sprite class and i have put the variables and objects from the Sprite class into the :base()

Here is my code:

**

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

1 回答 1

1

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

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

}

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

于 2013-09-16T21:33:07.980 回答