C#XNA - 移动动画矩形

时间:2014-10-15 14:45:17

标签: c# animation xna rectangles sprite-sheet

所以我使用player矩形为spritesheet制作动画。动画可以工作,但如果我尝试用箭头键移动精灵,它会将矩形移动到spritesheet中的下一个精灵。这是我的代码:

    Texture2D m_PlayerShipTex;
    Rectangle m_PlayerShipHitBox;
    public static Vector2 g_PlayerShipPos = new Vector2(400, 486);
    Vector2 m_PlayerShipOrigin;

    int m_PlayerShipCurrentFrame = 1;
    int m_PlayerShipFrameWidth = 62;
    int m_PlayerShipFrameHeight = 64;

    float m_Timer = 0f;
    float m_Interval = 100;

    public void LoadContent(ContentManager Content)
    {
        m_PlayerShipTex = Content.Load<Texture2D>(".\\gameGraphics\\gameSprites\\playerShip\\playerShipSpriteSheet");
    }

    public void Update(GameTime gameTime)
    {
        m_PlayerShipHitBox = new Rectangle(m_PlayerShipCurrentFrame * m_PlayerShipFrameWidth, 0, m_PlayerShipFrameWidth, m_PlayerShipFrameHeight);
        m_PlayerShipOrigin = new Vector2(m_PlayerShipHitBox.X / 2, m_PlayerShipHitBox.Y / 2);

        MouseState m_MouseState = Mouse.GetState();
        KeyboardState m_KeyboardState = Keyboard.GetState();

        m_Timer += (float)gameTime.ElapsedGameTime.Milliseconds;

        if (m_Timer > m_Interval)
        {
            m_PlayerShipCurrentFrame++;
            m_Timer = 0f;
        }

        if (m_PlayerShipCurrentFrame == 2)
        {
            m_PlayerShipCurrentFrame = 0;
        }

        m_PlayerShipHitBox = new Rectangle(m_PlayerShipCurrentFrame * m_PlayerShipFrameWidth, 0, m_PlayerShipFrameWidth, m_PlayerShipFrameHeight);
        m_PlayerShipOrigin = new Vector2(m_PlayerShipHitBox.Width / 2, m_PlayerShipHitBox.Height / 2);

        if (m_KeyboardState.IsKeyDown(Keys.Right))
        {
            g_PlayerShipPos.X += 3;
        }

        if (m_KeyboardState.IsKeyDown(Keys.Left))
        {
            g_PlayerShipPos.X -= 3;
        }

        if (m_KeyboardState.IsKeyDown(Keys.Down))
        {
            g_PlayerShipPos.Y += 3;
        }

        if (m_KeyboardState.IsKeyDown(Keys.Up))
        {
            g_PlayerShipPos.Y -= 3;
        }

        if (g_PlayerShipPos.X <= 0)
        {
            g_PlayerShipPos.X = 0;
        }

        if (g_PlayerShipPos.X + m_PlayerShipTex.Width >= 1110)
        {
            g_PlayerShipPos.X = 1110 - m_PlayerShipTex.Width;
        }

        if (g_PlayerShipPos.Y <= 0)
        {
            g_PlayerShipPos.Y = 0;
        }

        if (g_PlayerShipPos.Y + m_PlayerShipTex.Height >= 600)
        {
            g_PlayerShipPos.Y = 600 - m_PlayerShipTex.Height;
        }

    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(m_PlayerShipTex, g_PlayerShipPos, m_PlayerShipHitBox, Color.White, 0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
    }

如何move the rectangle while it animates the sprites

0 个答案:

没有答案