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
Greg XavierPosted Jul 15, 2012, 3:09 AM
ShipSprite = new AIEStaticSprite(spriteBatch, Content, "Spaceship", new Vector2(100.0f, 100));
Perhaps that's the problem? So it needs to be a Vector2 for me to be able to manipulate it?
I think I see what you are saying though. I will have another play this evening and see if I can get it working. Thank you frogleg. :)
FroglegPosted Jul 14, 2012, 4:40 PM
If so you need a vector2
ie
Vector2 ShipPosition = new Vector2(100, 100);
in update
ShipPosition.X += 2;//move sprite right
if(ShipPosition.X >= GraphicsDevice.Viewport.Width)
{
ShipPosition.X -= 2;// // move sprite back if at bounds
}
in draw
spriteBatch.Draw(ShipSprite, ShipPosition, Color.White);