如何实现精灵动画

示范如何用自定义类使从纹理创建的精灵活动。

本例中的代码假定加载的纹理是大小相同的4个图像拼接成的一个大小为256X64的纹理(其实是将动画精灵的4个帧作为4个64X64的纹理横着拼在了一起)。示例使用一个叫做AnimatedTexture的类,它包含在示例中。

绘制活动的精灵

绘制活动的精灵

  1. 按照如何绘制精灵中所讲述的1-3步进行操作。

  2. 在构造函数中创建AnimatedTexture 类的实例

    在本例中使用(0,0)作为精灵原点,不旋转,2倍缩放,0.5的深度值。

            private AnimatedTexture SpriteTexture;
            private const float Rotation = 0;
            private const float Scale = 2.0f;
            private const float Depth = 0.5f;
            public Game1()
            {
                ...
                SpriteTexture = new AnimatedTexture(Vector2.Zero, Rotation, Scale, Depth);
    #if ZUNE
                // Frame rate is 30 fps by default for Zune.
                TargetElapsedTime = TimeSpan.FromSeconds(1 / 30.0);
    #endif
            }
  3. 加载提供精灵活动数据的纹理。

    在本例中,AnimatedTexture 类加载几个单一的纹理,并且把他分成精灵的不同的帧动作。 最后一个参数用来确定每秒钟绘制多少帧。在本例中,将在2fps(帧每秒) 的情况下绘制一个包含4帧的精灵。

    private Viewport viewport;
    private Vector2 shipPos;
    private const int Frames = 4;
    private const int FramesPerSec = 2;
    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);
    
        // "shipanimated" is the name of the sprite asset in the project.
        SpriteTexture.Load(Content, "ship", Frames, FramesPerSec);
        viewport = graphics.GraphicsDevice.Viewport;
        shipPos = new Vector2(viewport.Width / 2, viewport.Height / 2);
    }
  4. 在你的Game类中的Update方法里确定要显示哪一帧。

    protected override void Update(GameTime gameTime)
    {
        ...
        float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
    
        // TODO: Add your game logic here.
        SpriteTexture.UpdateFrame(elapsed);
        base.Update(gameTime);
    }

    这是由 AnimatedTexture类的UpdateFrame 方法处理的,这个方法使用更新一帧流逝的秒数作为参数。

    // class AnimatedTexture
    public void UpdateFrame(float elapsed)
    {
        if (Paused)
            return;
        TotalElapsed += elapsed;
        if (TotalElapsed > TimePerFrame)
        {
            Frame++;
            // Keep the Frame between 0 and the total frames, minus one.
            Frame = Frame % framecount;
            TotalElapsed -= TimePerFrame;
        }
    }
  5. 在你的Game类中的Draw方法中, 调用 AnimatedTexture 实例的DrawFrame方法。

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
    
        // TODO: Add your drawing code here
        spriteBatch.Begin();
        SpriteTexture.DrawFrame(spriteBatch, shipPos);
        spriteBatch.End();
    
        base.Draw(gameTime);
    }

    AnimatedTexture 将用我们加载的纹理中的4个子块来轮流绘制精灵,达到我们期望的效果。 

    // class AnimatedTexture
    public void DrawFrame(SpriteBatch batch, Vector2 screenPos)
    {
        DrawFrame(batch, Frame, screenPos);
    }
    public void DrawFrame(SpriteBatch batch, int frame, Vector2 screenPos)
    {
        int FrameWidth = myTexture.Width / framecount;
        Rectangle sourcerect = new Rectangle(FrameWidth * frame, 0,
            FrameWidth, myTexture.Height);
        batch.Draw(myTexture, screenPos, sourcerect, Color.White,
            Rotation, Origin, Scale, SpriteEffects.None, Depth);
    }

发布时间:2009/2/11 12:36:35  阅读次数:4979

2006 - 2024,推荐分辨率1024*768以上,推荐浏览器Chrome、Edge等现代浏览器,截止2021年12月5日的访问次数:1872万9823 站长邮箱

沪ICP备18037240号-1

沪公网安备 31011002002865号