【问题标题】:Having problems drawing animations in monogame在MonoGame中绘制动画问题
【发布时间】:2014-03-22 21:02:39
【问题描述】:

我正在尝试创建具有多种动画(空闲、攻击、损坏等)的宇宙飞船 但是当我尝试更改动画时,我会在“旧”位置(上次更改动画的位置)上看到当前动画的第一帧一瞬间

说明图: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 是一个布尔变量。当'Space'被按下时,变量的值变为TRUE,当'Space'被释放时,值为FALSE

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

【问题讨论】:

    标签: c# animation sprite monogame sprite-sheet


    【解决方案1】:

    我对确切的问题有点不清楚,所以如果我不明白,也许你可以帮助澄清一下。

    但是根据你的图片,当你按下空格键开火时,玩家动画应该改变为用蓝色火焰点亮他的枪(或者它是:))而且看起来它正在这样做,但它的位置错误(在生命条上方 - 我看到它发射的激光在正确的位置:))

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

    所以这意味着玩家射击的动画使用了错误的位置。

    你是:

    1. 在玩家更新中更新玩家射击动画的位置?
    2. 在玩家更新中调用玩家射击动画的Update

    所以你应该在你的播放器的某个地方有这个Update

    PlayerAnimation.Position = Position;
    PlayerAnimation.Update(gameTime);
    

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

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

    【讨论】:

    • 非常感谢您的回复。我做了一个不同的修复。我将 Animation 类中的“spriteStrip”变量设为公共变量。然后我没有改变播放器的动画,而是改变了精灵表。
    • 酷。此外,值得一试 cocos2d-xna。我和你一样在 Monogame 中制作游戏,并将我的代码移植到使用 Monogame 和 cocos2d-xna。它使这样的东西更容易掌握。查看 TexturePacker,它可以让你为 cocos2d-xna 制作精灵表。祝你好运!
    • monogame和cocos2d-xna有什么区别?我必须更改很多代码吗?
    • cocos2d-xna 和monogame 非常相似(都是开源游戏框架),但可以结合使用以达到两全其美的效果。 Cocos 也有 Box2D 支持(一个物理引擎),但现在这可能太超前了。 Cocos 提供了一个非常广泛的 API 来涵盖许多东西,例如精灵、动画、图层、动作和场景。而且您不必更改太多代码。只需让您的 Player 和屏幕分别继承 CCSprite 和 CCLayer 并查看教程以了解您可以实现哪些功能!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-08
    • 2011-05-16
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多