Sprite disappears after adding a code

84 Views Asked by At
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)

image link:https://i.stack.imgur.com/3MVPv.png

2

There are 2 best solutions below

0
Haedrian On

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).

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

should do what you were expecting

0
pinckerman On

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;