XNA:Basic DrawableGameComponent覆盖未调用的函数(Update,Draw等)

时间:2010-08-22 02:45:24

标签: c# xna

我正在尝试通过扩展Mircosoft.Xna.Framework.GameCompenent来封装我的游戏对象,然后仅构建它们并在Game1的Update()方法中管理它们。我有我的Game1类,Player类和Animation类。假设动画管理对象的Texture2D更改,在本例中为Player。

我的问题是,即使我已经成功扩展了所有内容,没有语法错误,没有抛出异常,并且已经检查并重新检查了我编写的代码,所以不会调用覆盖函数,我最终得到了黑屏。

Game1.cs :(请注意,只有两行改变了玩家声明)

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    Player player;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    protected override void Initialize()
    {
        player = new Player(this);

        base.Initialize();
    }

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
    }

    protected override void UnloadContent()
    {

    }

    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        // TODO: Add your update logic here

        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.Black);
        base.Draw(gameTime);
    }
}

Player.cs:

class Player : Microsoft.Xna.Framework.DrawableGameComponent
{
    Rectangle bounds;
    Texture2D t;
    Animation[] animations = new Animation[4];
    String path = @"..\..\..\Content\player.png";

    #region Animation Constants
    private const int WALK_RIGHT = 0;
    #endregion

    SpriteBatch spriteBatch;

    public Player(Game game) : base(game)
    {
        //should only ever be one player, all value defaults set in Initialize()
    }

    public Texture2D T
    {
        get { return t; }
    }

    public Rectangle Bounds
    {
        get { return bounds; }
    }

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

        bounds = new Rectangle(0, 0,
            System.Drawing.Image.FromFile(path).Width,
            System.Drawing.Image.FromFile(path).Height
        );

        t = Game.Content.Load<Texture2D>("player");
        animations[0] = new Animation(this.Game, "player", "walking", 3);
    }

    protected override void LoadContent()
    {
        base.LoadContent();

        spriteBatch = new SpriteBatch(this.Game.GraphicsDevice);
    }

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

        KeyboardState k = Keyboard.GetState();

        if (k.IsKeyDown(Keys.Right)) //walk right
        {
            bounds.X += 3;
            if (animations[WALK_RIGHT].Playing)
            {
                t = animations[WALK_RIGHT].getTexture();
            }
            else
            {
                animations[WALK_RIGHT].Play();
            }
        }
        else if (animations[WALK_RIGHT].Playing)
            animations[WALK_RIGHT].Stop();

    }

    public override void Draw(GameTime gameTime)
    {
        base.Draw(gameTime);

        spriteBatch.Begin();
        spriteBatch.Draw(t, bounds, Color.White);
        spriteBatch.End();
    }
}

Animation.cs:

class Animation : Microsoft.Xna.Framework.GameComponent
{
    Game game;
    String name; //name of default sprite; standing, unmoving, neutral, etc. The rest of the animation sprite names should derive from this
    String keyword;
    int frameCount;
    int delay; //frames between texture change


    String[] paths; //texture pathnames generated by the MakePaths() function
    int currentFrame = 0;
    int delayCount = 0;
    bool playing = false;

    public Animation(Game associatedGame, String nameVal, String keywordVal, int frameCountVal)
        : base(associatedGame)
    {
        name = nameVal;
        keyword = keywordVal;
        frameCount = frameCountVal;
        paths = MakePaths();
        delay = 10;
    }

    public Animation(Game associatedGame, String nameVal, String keywordVal, int frameCountVal, int delayVal)
        : base(associatedGame)
    {
        name = nameVal;
        keyword = keywordVal;
        frameCount = frameCountVal;
        paths = MakePaths();
        delay = delayVal;
    }

    private String[] MakePaths()
    {
        //format: name_keyword_anim[i]
        //format example: player_walking_anim1

        String[] temp = new String[frameCount];
        for (int i = 0; i < frameCount; i++)
        {
            temp[i] = name + "_" + keyword + "_" + "anim" + i.ToString();
        }

        return temp;
    }

    public Texture2D getTexture()
    {
        return Game.Content.Load<Texture2D>(paths[currentFrame]);
    }

    public void Play()
    {
        playing = true;
    }

    public void Stop()
    {
        currentFrame = 0;
        delayCount = 0;
        playing = false;
    }

    public bool Playing
    {
        get { return playing; }
    }

    public override void Update(GameTime gameTime)
    {
        if (playing)
        {
            if (delayCount == delay)
            {
                delayCount = 0;

                if ((currentFrame + 1) == frameCount) currentFrame = 0;
                else currentFrame++;
            }
            else delayCount++;
        }
        base.Update(gameTime);
    }

    public override string ToString()
    {
        return "params: " + name + "," + keyword + "," + frameCount.ToString() + "\nUsing paths: " + paths;
    }
}

调用的唯一LoadContent,Initialize,Update和Draw方法是Game1中的方法。让我感到困惑的是,我之前能够毫无问题地使用这种技术。这些函数将由Xna更新过程自然调用。

所以......为什么会这样?

1 个答案:

答案 0 :(得分:14)

您需要将游戏组件添加到Components集合中以自动调用它们

protected override void Initialize()
{
    player = new Player(this);
    Components.Add(player);

    base.Initialize();
}

请参阅http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.game.components.aspx