4

我想知道是否可以将对象与其实例名称匹配。

我有 :

class AnimatedEntity : DrawableEntity
{
    Animation BL { get; set; }
    Animation BR { get; set; }
    Animation TL { get; set; }
    Animation TR { get; set; }
    Animation T { get; set; }
    Animation R { get; set; }
    Animation L { get; set; }
    Animation B { get; set; }

    Orientation orientation ;

    public virtual int Draw(SpriteBatch spriteBatch, GameTime gameTime)
    {
        //draw depends on orientation
    }
}

enum Orientation { 
    SE, SO, NE, NO, 
    N , E, O, S, 
    BL, BR, TL, TR, 
    T, R, L, B 
}

Orientation 是一个枚举,Animation 是一个类。

我可以用相同的名称从方向调用正确的动画吗?

4

2 回答 2

3

与其将动画存储在属性中,不如使用字典?

Dictionary<Orientation, Animation> anim = new Dictionary<Orientation, Animation> {
    { Orientation.BL, blAnimation },
    { Orientation.BR, brAnimation },
    { Orientation.TL, tlAnimation },
    { Orientation.TR, trAnimation },
    { Orientation.T, tAnimation },
    { Orientation.R, rAnimation },
    { Orientation.L, lAnimation },
    { Orientation.B, bAnimation }
};

然后您可以使用anim[orientation]来访问相应的动画。

于 2013-03-01T00:41:58.260 回答
1

确实 aDictionary将是一个不错的选择。Animation如果动画是从外部设置的,它甚至可以有一个索引:

class AnimatedEntity : DrawableEntity
{
    Dictionary<Orientation, Animation> Animations { get; set; }

    public AnimatedEntity()
    {
        Animations = new Dictionary<Orientation, Animation>();
    }

    public Animation this[Orientation orientation] 
    { 
        get{ return Animations[orientation]; }
        set{ Animations[orientation] = value;}
    }

    Orientation Orientation { get; set; }

    public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
    {
        Animation anim = Animations[Orientation];
    }
}

会像这样使用:

AnimatedEntity entity = new AnimatedEntity();
entity[Orientation.B] = bAnimation;
entity[Orientation.E] = eAnimation;
entity[Orientation.SE] = seAnimation;
于 2013-03-01T01:05:58.433 回答