简体   繁体   English

添加代码后精灵消失

[英]Sprite disappears after adding a code

protected override void Initialize()
{
    graphics.PreferredBackBufferWidth = 720;
    graphics.PreferredBackBufferHeight = 1080;
    graphics.IsFullScreen = true;
    graphics.ApplyChanges();
    Window.Title = "Game";
    base.Initialize();
}

protected override void LoadContent()
{
    spriteBatch = new SpriteBatch(GraphicsDevice);
    Texture = Content.Load<Texture2D>("Sprite");
    position.X = 1;
    position.Y = 520;
}

protected override void Update(GameTime gameTime)
{
    if (Keyboard.GetState().IsKeyDown(Keys.Down))
        position.Y += 2;
    if (Keyboard.GetState().IsKeyDown(Keys.Up))
        position.Y -= 2;
    if (Keyboard.GetState().IsKeyDown(Keys.Right))
        position.X+= 2;
    if (Keyboard.GetState().IsKeyDown(Keys.Left))
        position.X -= 2;
    if (position.Y > 520)
        position.Y = 520;

    base.Update(gameTime);
}

and when I add 当我添加

if (position.Y < 1080)
   position.Y = 1080;

the sprite disappears (sorry for posting such a long code I did not know what I did wrong.) (to save space I removed most of the white space) Sprite消失了(很抱歉张贴这么长的代码,我不知道自己做错了。)(为节省空间,我删除了大部分空白)

image link: http://i.imgur.com/WZwpGeE.png 图片链接: http : //i.imgur.com/WZwpGeE.png

Your comparator is flipped. 您的比较器被翻转。

You're checking if the y position is SMALLER than 1080, then putting it at 1080 (the edge of the screen). 您正在检查y位置是否比1080小,然后将其置于1080(屏幕边缘)。

if (position.Y > 1080)
{
position.Y = 1080;
}

should do what you were expecting 应该做你期望的

You are doing 你在做

if (position.Y < 1080)
   position.Y = 1080;

and that's wrong, because you are setting that position out of your screen. 这是错误的,因为您正在将该位置设置在屏幕之外。 Maybe you mean this: 也许你的意思是这样的:

if (position.Y > 1080)
   position.Y = 1080;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM