动画在XNA中不起作用

时间:2016-01-08 10:42:25

标签: c# animation xna monogame

我上课时切掉了大片spritesheet的部分并将其绘制在屏幕上。 而且我设法绘制了那部分不会有动画的部分。

    public AnimatedSprite(Vector2 position, string texture)
    {
        sPostion = position;
        sTexture = Asset_Manager.GetInstance().texture[texture];
}

    /// <summary>
    /// Adds an animation to the AnimatedSprite
    /// </summary>
    public void AddAnimation(int frames, int yPos, int xStartFrame, string name, int width, int height, Vector2 offset)
    {

        //Creates an array of rectangles which will be used when playing an animation
        Rectangle[] Rectangles = new Rectangle[frames];

        //Fills up the array of rectangles
        for (int i = 0; i < frames; i++)
        {
            Rectangles[i] = new Rectangle((i + xStartFrame) * width, yPos, width, height);
        }
        sAnimations.Add(name, Rectangles);
        sOffsets.Add(name, offset);
    }

    /// <summary>
    /// Determines when we have to change frames
    /// </summary>
    /// <param name="GameTime">GameTime</param>
    public virtual void Update(GameTime gameTime)
    {

        timeElapsed += gameTime.ElapsedGameTime.TotalSeconds;


        if (timeElapsed > timeToUpdate)
        {

            timeElapsed -= timeToUpdate;


            if (frameIndex < sAnimations[currentAnimation].Length - 1)
            {
                frameIndex++;
            }
            else //Restarts the animation
            { 
                frameIndex = 0;
            }
        }
    }


    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(sTexture, sPostion, sAnimations[currentAnimation][frameIndex], Color.White);
    }

    public void PlayAnimation(string name)
    {

        if (currentAnimation != name)
        {
            currentAnimation = name;

        }
    }


}

}

动画Class

玩家Class

它不会循环播放动画。

public class Player
{

    AnimatedSprite player;

    public Player()
    {

        player = new AnimatedSprite(new Vector2(100,100),"Ships");
        player.AddAnimation(2, 0, 1, "GreenPlane", 25, 26, new Vector2(0, 0));
        player.PlayAnimation("GreenPlane");
    }
    public void Update(GameTime gameTime)
    {
        player.Update(gameTime);   
    }
    public void Draw(SpriteBatch spriteBach)
    {
        player.Draw(spriteBach);
    }

它绘制第一帧,它不会从第1帧变为第x帧并循环回来,

1 个答案:

答案 0 :(得分:-1)

5天前被问到,所以我认为你已经有了解决方案..

如果没有: 你的代码看起来很好。我把你的代码放在一个测试项目中工作,它更新并呈现得很好。在你的片段中你在玩家(AnimatedSprite类型)上调用.Update(GameTime),但你是否也在播放器实例(类型为Player)本身上调用了.Update(GameTime)?

-

我尝试了你的链接课程和纯粹的&#39; xna - 不是单声道