Greg Xavier

Greg Xavier

  • NA
  • 3
  • 2.6k

Simple wrapping

Jul 14 2012 5:39 AM
I'm just starting out in C# XNA games programming.

In our last class we were shown a very simple way of making a sprite wrap from one side of the screen to the other. I can't recall how we did this and unfortunately we were told we would not need to take notes.

I have searched the web for solutions but they are all very complex compared to what we were shown, which was something along the line of taking the position of the sprite and, if it hit one edge of the screen, we reduced it's location either to 0 or to the max height/width of the viewport as appropriate.

I have tried to replicate it from memory but I'm obviously missing something fundamental as it wont let me modify the sprite.position as it's not a variable. Here's my (fail) code.

            //wrap sprites from one side of the screen to the other

            if (ShipSprite.Position.X > GraphicsDevice.Viewport.Width)
                ShipSprite.Position.X = 0;


            if (ShipSprite.Position.X < 0)
                ShipSprite.Position.X = GraphicsDevice.Viewport.Width;


            if (ShipSprite.Position.Y > GraphicsDevice.Viewport.Height)
                ShipSprite.Position.Y = 0;


            if (ShipSprite.Position.Y < 0)
                ShipSprite.Position.Y = GraphicsDevice.Viewport.Height;

I've been bashing my head against this all afternoon and just can't figure out what it is I'm doing wrong. Like I said, I am very new so I expect it is something pretty fundamental. But I would hugely appreciate a nudge in the right direction.

Thanks!
Greg

Answers (2)