C# 使用游戏组件和IDrawable的XNA精灵类

C# 使用游戏组件和IDrawable的XNA精灵类,c#,xna-4.0,C#,Xna 4.0,我正在为一个游戏学习精灵和演员课程,我有两种不同类型的敌人,一个南瓜和一只蝙蝠。我想做的是使它们都是可更新的,这样南瓜就会从它的卵中直接掉下来,蝙蝠就会跟着玩家直到玩家死亡或毁灭。但现在我正在尝试创建他们的Actor类,我对XNA和堆栈溢出有点陌生 雪碧类: public class Sprite : Microsoft.Xna.Framework.GameComponent, IDrawable { public Texture2D texture; public Vector

我正在为一个游戏学习精灵和演员课程,我有两种不同类型的敌人,一个南瓜和一只蝙蝠。我想做的是使它们都是可更新的,这样南瓜就会从它的卵中直接掉下来,蝙蝠就会跟着玩家直到玩家死亡或毁灭。但现在我正在尝试创建他们的Actor类,我对XNA和堆栈溢出有点陌生

雪碧类:

public class Sprite : Microsoft.Xna.Framework.GameComponent, IDrawable
{
    public Texture2D texture;
    public Vector2 position;
    public Vector2 moveSpeed;
    public Vector2 scale;
    public Color tint;
    public bool active;

    public Rectangle rect
    {
        get { return new Rectangle((int)position.X, (int)position.Y, Width, Height); }
    }
    public int Height
    {
        get { return (int)(texture.Height * scale.Y); }
    }
    public int Width
    {
        get { return (int)(texture.Width * scale.X);}
    }
    public Sprite(Game game, String tex)
        : base(game)
    {
        // TODO: Construct any child components here
        texture = game.Content.Load<Texture2D>(tex);
    }
    /// <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();
        position = Vector2.Zero;
        tint = Color.White;
        active = true;
    }
    /// <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)
    {
        // TODO: Add your update code here
        base.Update(gameTime);
    }
    public void Draw(GameTime gameTime)
    {
        ((Game1)Game).spriteBatch.Begin();
        ((Game1)Game).spriteBatch.Draw(texture,rect,tint);
        ((Game1)Game).spriteBatch.End();
    }
    public int DrawOrder
    {
        get { return 1; }
    }
    public event EventHandler<EventArgs> DrawOrderChanged;
    public bool Visible
    {
        get { return active; }
    }
    public event EventHandler<EventArgs> VisibleChanged;
}
演员级别:

public class Actor : Microsoft.Xna.Framework.GameComponent
{
    public Sprite sprite; //Call Instance of Sprite class
    public String tag; //String for the texture
    public float health; //Health of the sprite
    public int value; //The enemies will need a score

    public Actor(Game game, String tex)
        : base(game)
    {
        // TODO: Construct any child components here
        sprite = new Sprite(game, tex);

        game.Components.Add(this);
    }


    /// <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();
        health = 100.0f; //Initialize the health of Enemy
        value = 10; //Initialize the value of enemy destruction
    }

    /// <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)
    {
        // TODO: Add your update code here
        if(sprite.rect.Intersects(sprite.rect))
        {
            health -= 100;
            sprite.active = false;
        }

        base.Update(gameTime);
    }
}

你的精确编程问题到底是什么?很难说你的问题是什么。顺便说一下,你可以使用DrawableGameComponent,而不是GameComponent和IDrawable。我一直在使用actor类。我不知道如何实施,也不知道我是否真的做对了,我还检查了我是否应该将敌人的生命值和分数值放在actor中,或者我应该为这两类敌人使用一个管理器。在我看来,将他们留在他们的actor类中是可以的。或者你可以为你的两个敌人添加两个继承actor的类,并将它们的值存储在那里。我想从长远来看,它们也会有不同的行为,所以你需要一种方法来区分它们。如果你的设计需要灵感,你也可以检查这个伟大的。您可以下载源代码并查看它,它提供了您入门所需的一切,并在网站上提供了基本的解释。感谢您的建议,因为您是对的,他们会有不同的行为。我也在看你现在发布的链接,谢谢。