//update the camera movement per frame after it has confirmed the layer's previous position public void Update(GameTime gameTime) { float elapsedSeconds = (float)gameTime.ElapsedGameTime.TotalSeconds; float previousPosX = position.X; //previous X position of the layer position.X -= ScrollingSpeed * elapsedSeconds; //set the new position of the layer for (int i = 0; i < level.backgroundLayers.Length; i++) //for every item in the backgroundLayer array in Level { //create an instance of it BackgroundLayer layer = level.backgroundLayers[i]; //access the ScrollSpeed property of BackgroundLayer and set OffsetX property level.backgroundLayers[i].OffsetX -= layer.Texture.Width; if (level.backgroundLayers[i].OffsetX < layer.Texture.Width) { level.backgroundLayers[i].OffsetX = 0; } } }
//Finally, a level instance public Level(Main game) { //Pass the Main in this.game = game; //create an instance of the player player = new Player(game); //Set the backgroun up with the array and load in the content backgroundLayers = new BackgroundLayer[4]; backgroundLayers[0] = new BackgroundLayer() // call that autogenerated empty constructor in BackgroundLayer { Texture = game.Content.Load <Texture2D>("_background/Layer1"), ScrollingSpeed = 0.25f, OffsetX = 300 }; backgroundLayers[1] = new BackgroundLayer() // call that autogenerated empty constructor in BackgroundLayer { Texture = game.Content.Load <Texture2D>("_background/Layer2"), ScrollingSpeed = 0.5f, OffsetX = 700 }; backgroundLayers[2] = new BackgroundLayer() // call that autogenerated empty constructor in BackgroundLayer { Texture = game.Content.Load <Texture2D>("_background/Layer3"), ScrollingSpeed = 0.75f, OffsetX = 800 }; backgroundLayers[3] = new BackgroundLayer() // call that autogenerated empty constructor in BackgroundLayer { Texture = game.Content.Load <Texture2D>("_background/Layer9"), ScrollingSpeed = 1f, OffsetX = 900 }; //Create an instance of Camera Camera = new Camera(this); }