Domanda

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);
        }
    }
}
È stato utile?

Soluzione

The syntax of the constructor your inherited class is wrong. I suspect you want:

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

}

If you want a parameterless constructor then you're going to have to come up with the values for the parameters yourself (or not call the base constructor).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top