C#XNA - 雪碧移出窗口/屏幕 [英] C# XNA - Sprite moves out of window/screen

查看:182
本文介绍了C#XNA - 雪碧移出窗口/屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我按下向左或向上箭头键精灵获得窗外/屏幕。我的代码:

When I press left or up arrow keys the sprite gets out of the window/screen. My code:

    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);
    }



我不知道什么可能是错的,我的窗口是800×600,但如果我设置 m_PlayerShipTex.Width> = 800 我可以只在屏幕的一半,这就是为什么我使用1141也是一样的窗口的高度......我是什么我做错了,为什么船舶出reacheable区域的?

I don't know what could be wrong, my window is 800x600 but if I set m_PlayerShipTex.Width >= 800 I can get only to half of the screen, that's why I'm using 1141. Same goes for window height... What am I doing wrong and why the ship's out of the "reacheable" area?

推荐答案

要解决原来的问题,使用绘制函数:

To fix the original problem, use this draw function:

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)为原点的图纸;这意味着你的边缘的计算将随着工作的原先预期。如果你想在中心原点,那么你需要计算的边缘时,以说明调整

This will use (0,0) as the origin for drawing; which means that your "edge" calculations will work as originally expected. If you want the centered origin, then you need to account for that adjustment when calculating the edges.

要更改原点的唯一原因是为了更容易转动;一般来说,你可以使用 Vector2.Zero

The only reason to change the origin is to make rotation easier; in general, you can just use Vector2.Zero.

这篇关于C#XNA - 雪碧移出窗口/屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆