How do I create a scrolling X and scrolling Y background without weird things happening like the player disappearing or not getting the offset correct?
I have code at https://vanillity.kilnhg.com/Code/Repositories/Group/Aremac
Looks like this:
var s = game.gd.Viewport.Bounds; var g = game.world.Bounds; var p = game.player.Bounds; var v = viewRectangle; var x = p.X 0 ? p.X - s.Width/2 : v.Right >= 1090 ? 90 : p.X > s.Width/2 ? p.X - s.Width/2 : 0; var y = //v.Bottom > p.Y > s.Height/2 ? p.Y - s.Height/2: 0; var w = s.Width; var h = s.Height; //view Rectangle Offset = new Vector2(x , y); viewRectangle = new Rectangle( x, y ,w, h);
and then:
//source rectangle var x = (int)0; var y = (int)0; var g = game.world.Bounds; var w = g.Width; var h = g.Height; var rect = new Rectangle(x,y,w,h); spriteBatch.Draw( w.boundsTexture ,rect ,viewRectangle ,w.Color ,0 ,Vector2.Zero ,SpriteEffects.None ,1 );
Before center, the grey guy is holding the rock in his left hand:
After center, the rock (and the white guy) drifts evermore to the right: