开发者

xna multilevel inheritance - cannot access base-base class properties

开发者 https://www.devze.com 2023-03-30 16:41 出处:网络
Title should be clear, I\'m having trouble accessing the lowest level properties with multiple inheritance.

Title should be clear, I'm having trouble accessing the lowest level properties with multiple inheritance.

Object A extends Object B Object B extends Object C

Object C has a property that I want to access from object A, but I can only acces it from object B for some reason. Goes for both variables and functions.

I'm using a custom libbrary - "Windows Game Library(4.0)" to be exact. I never had any problems with this when not-using a library. The only difference with now and then were that I'm using the "public" keyword on classes in the library now, because I would get an "inaccessable" error otherwise.

In code:

Object A

    namespace ExampleGame
{
    class Player : Actor
    {

    public Player()
    {
        //most things happen in gameobject<actor<this
        MaxSpeed = new Vector2(10, 10);
        Acceleration = new Vector2(5, 5);
        Velocity = new Vector2(0, 0);
        MaxJumpPower = 15;
    }


    override public void Update()
    {
        base.Update();

        manageInput();
    }

}
}

Object B

namespace GridEngineLibrary.objects
{
 public class Actor : GameObject
    {
    public int MaxJumpPower;

    public Actor()
    {
        canMove = true;
    }

    /// <summary>
    /// moves the object but it's acceleration
    /// </summary>

    public void jump()
    {

        if (grounded == true)
        {
            Console.WriteLine("jump!");
            Direction.Y = -1;
            Velocity.Y = MaxJumpPower * -1;
        }
    }

}
}

Object C

    namespace GridEngineLibrary.objects
{
    public class GameObject
    {
        public Vector2 location;
        public SpriteBatch spritebatch;
        public Vector2 hitArea;
        public AnimatedTexture graphic;

    public Vector2 Velocity;
    public Vector2 Acceleration;
    public Vector2 MaxSpeed;
    public Vector2 Direction;
    public int Z = 1;

    public bool canMove;

    public GameObject()
    {
        spritebatch = SpriteManager.spriteBatch;
    }

    /// <summary>
    /// set the animated texture 
    /// </summary>
    开发者_Python百科/// <param name="location"></param>
    /// <param name="size"></param>
    /// <param name="TextureName">name of texture to load</param>
    public void setAnimatedTexture(Vector2 location, Vector2 size, string TextureName, 
                                    int totalFrames, int totalStates, int animationSpeed = 8, 
                                    int spacing = 9)
    {
        graphic = new AnimatedTexture(location, size, TextureName);
        graphic.isAnimated = true;
        graphic.totalStates = totalStates;
        graphic.totalFrames = totalFrames;
        graphic.animationSpeed = animationSpeed;
        graphic.spacing = spacing;

        hitArea = size;
    }


    virtual public void Update()
    {
        graphic.update(location);
    }
}

}


This is by no means an expert answer, but you could always just expose the properties you want from Class C in Class B and thereby access them from base in class A.

I'm sure there is a more elegant solution, but just sort of mirroring (or perhaps it would be called tunneling?) the properties through B should at least be serviceable.


There might be an Actor class in the ExampleGame namespace or some namespace imported by a using directive that Player is inheriting from. This would not be the same Actor class as is in the GridEngine.objects namespace.

0

精彩评论

暂无评论...
验证码 换一张
取 消