C#XNA - Sprite移出窗口/屏幕

时间:2014-10-15 16:27:28

标签: c# xna

当我按向左或向上箭头键时,精灵会离开窗口/屏幕。我的代码:

    Texture2D m_PlayerShipTex;
    Rectangle m_PlayerShipHitBox;
    Vector2 m_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))
        {
            m_PlayerShipPos.X += 3;
        }

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

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

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

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

        if (m_PlayerShipPos.X + m_PlayerShipTex.Width >= 1141)
        {
            m_PlayerShipPos.X = 1141 - m_PlayerShipTex.Width;
        }

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

        if (m_PlayerShipPos.Y + m_PlayerShipTex.Height >= 620)
        {
            m_PlayerShipPos.Y = 620 - m_PlayerShipTex.Height;
        }

    }

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

我不知道会出现什么问题,我的窗口是800x600但如果我设置m_PlayerShipTex.Width >= 800我只能看到屏幕的一半,这就是我使用1141的原因。窗口高度也是如此。 ..我做错了什么以及为什么船离开"reacheable"区域?

1 个答案:

答案 0 :(得分:0)

要解决原始问题,请使用此绘制功能:

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

这将使用(0,0)作为绘图的原点;这意味着您的“边缘”计算将按原先的预期工作。如果您想要居中的原点,则需要在计算边缘时考虑该调整。

更改原点的唯一原因是使旋转更容易;通常,您只需使用Vector2.Zero