forum

Home / DeveloperSection / Forums / How to subclass member initialization?

How to subclass member initialization?

Jayden Bell 1998 27-Jan-2015

I've created two classes which replace the function of a Point (both variants) just to test them out, this is how I've created them:

Entity.cs:

namespace Xnagame
{
 
    class Player
    {
 
        public class float2
        {
            public float X { get; set; }
            public float Y { get; set; }
        }
        public class int2
        {
            public int X { get; set; }
            public int Y { get; set; }
        }
        public int2 Position { get; set; }
        public int angle { get; set; }
        public float speed { get; set; }
        public float2 velocity { get; set; }
 
        public Player CreatePlayer()
        {
 
            Position = new Player.int2();
            Player player = new Player();
            return player;
        }
    }
}

As you can see, both Point classes have a X and Y variable (I'm not sure if this is the way to set them up, if it isn't please tell me) and these classes are used in the position and velocity instances which are instantiated in the CreatePlayer() method.

Game1.cs:

namespace Xnagame
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        private Texture2D playerimage;
        private Texture2D background;
        Player player = Player.CreatePlayer();

Now the problem is that when CreatePlayer() method tries to return it's "player" to the local player it gives the:

An object reference is required for the non-static field, method, or property Xnagame.Player.CreatePlayer()

error.

I also tried it with the new keyword and it gave me:

Xnagame.Player.CreatePlayer() is a method but is used like a type.


Updated on 27-Jan-2015

Can you answer this question?


Answer

1 Answers

Liked By