在单一游戏中绘制动画时遇到问题

时间:2014-03-22 21:02:40

标签: c# animation sprite monogame sprite-sheet

我正在尝试创建一个具有多个动画的太空船(空闲,攻击,损坏等) 但是,当我尝试更改动画时,我会在" old"上看到当前动画的第一帧。位置(最后一次更改动画的位置)

图片解释:http://i.imgur.com/L3O8rsc.png

红色矩形应该是一个健康栏。

运行我的动画的课程:

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Test
{
    class Animation
    {
        Texture2D spriteStrip;
        float scale;
        int elapsedTime;
        int frameTime;
        int frameCount;
        int currentFrame;
        Color color;
        // The area of the image strip we want to display
        Rectangle sourceRect = new Rectangle();    
        // The area where we want to display the image strip in the game
        Rectangle destinationRect = new Rectangle();
        public int FrameWidth;
        public int FrameHeight;
        public bool Active;
        public bool Looping;
        public Vector2 Position;    
        public void Initialize(Texture2D texture, Vector2 position, int frameWidth, int frameHeight, int frameCount, int frametime, Color color, float scale, bool looping)
        {
            this.color = color;
            this.FrameWidth = frameWidth;
            this.FrameHeight = frameHeight;
            this.frameCount = frameCount;
            this.frameTime = frametime;
            this.scale = scale;
            Looping = looping;
            Position = position;
            spriteStrip = texture;
            elapsedTime = 0;
            currentFrame = 0;

            Active = true;
        }
        public void Update(GameTime gameTime)
        {
            if (Active == false) return;

            elapsedTime += (int)gameTime.ElapsedGameTime.TotalMilliseconds;
            if (elapsedTime > frameTime)
            {
                currentFrame++;
                if (currentFrame == frameCount)
                {
                    currentFrame = 0;
                    if (Looping == false)
                        Active = false;
                }
                elapsedTime = 0;
            }
            sourceRect = new Rectangle(currentFrame * FrameWidth, 0, FrameWidth, FrameHeight);
            destinationRect = new Rectangle((int)Position.X - (int)(FrameWidth * scale) / 2, (int)Position.Y - (int)(FrameHeight * scale) / 2, (int)(FrameWidth * scale), (int)(FrameHeight * scale));
        }
        public void Draw(SpriteBatch spriteBatch)
        {
            if (Active)
            {
                spriteBatch.Draw(spriteStrip, destinationRect, sourceRect, color);
            }
        }
    }
}

我编写整个代码的原因是因为我不知道问题出在哪里。 在我的Game1.cs课程中,我有:


全局变量:

Texture2D playerTexture;
Texture2D playerShootingTexure;
Animation playerAnimation = new Animation();
Animation ShootingAnimation = new Animation();

...

protected override void LoadContent()
{
        spriteBatch = new SpriteBatch(GraphicsDevice);
        playerTexture = Content.Load<Texture2D>("Graphics/Player/PlayerShip");
        playerShootingTexure = Content.Load<Texture2D>("Graphics/Player/Player_Shooting");
        playerAnimation.Initialize(playerTexture, Vector2.Zero, playerTexture.Width / 5, playerTexture.Height, 5, 50, Color.Wheat, 1f, true);
        ShootingAnimation.Initialize(playerShootingTexure, Vector2.Zero, playerShootingTexure.Width / 5, playerShootingTexure.Height, 5, 50, Color.WhiteSmoke, 0.75f, true);
}

我改变动画的地方:

protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        // Start drawing
        spriteBatch.Begin();
        if (Player_Attacking == false)
        {
            player.PlayerAnimation = playerAnimation;
        }
        else
        {
            player.PlayerAnimation = ShootingAnimation;
        }
        player.Draw(spriteBatch);
        spriteBatch.End();

        base.Draw(gameTime);
    }

注意:Player_Attacking是一个布尔变量。当&#39; Space&#39;按下时,变量的值变为TRUE,此时空格&#39;被释放,值为FALSE

请帮忙! 提前致谢! 对不起巨大的文字墙

1 个答案:

答案 0 :(得分:0)

我对确切的问题有点不清楚,所以也许你可以帮助澄清我是否不理解。

但根据你的照片,似乎当你按空间射击时,玩家动画应该改为用蓝色火焰照亮他的枪(或者是它是:)但它看起来就像这样做,但是它处于错误的位置(在健康栏上方 - 我看到它拍摄的激光位于正确的位置:))

然而,一旦拍摄完成,它就会自动纠正,如第二张照片所示(不再是蓝色火焰)。

因此,这意味着玩家射击的动画使用了错误的位置。

你是:

  1. 更新播放器拍摄动画在播放器更新中的位置?
  2. 在播放器更新中调用播放器拍摄动画Update
  3. 所以你应该在你的玩家Update

    中的某个地方
    PlayerAnimation.Position = Position;
    PlayerAnimation.Update(gameTime);
    

    另外,请尝试在Game1.cs的Update中移动要使用的Player动画的检查:

    protected override void Update(GameTime gameTime)
    {
        if (Player_Attacking == false)
        {
            player.PlayerAnimation = playerAnimation;
        }
        else
        {
            player.PlayerAnimation = ShootingAnimation;
        }
        player.Update(gameTime);
    }