

In order for these Backgrounds to fill our entire viewport area, they need to be drawn using a LinearWrap SamplerState. SpriteBatch.Draw(Texture, new Vector2(Viewport.X, Viewport.Y), Rectangle, Color.White, 0, Vector2.Zero, Zoom, SpriteEffects.None, 1) Public void Draw(SpriteBatch spriteBatch) Vector2 distance = direction * Speed * elapsed

Calculate the distance to move our image, based on speed Public void Update(GameTime gametime, Vector2 direction, Viewport viewport)įloat elapsed = (float) Public Background(Texture2D texture, Vector2 speed, float zoom) Calculate Rectangle dimensions, based on offset/viewport/zoom values Private Viewport Viewport //Our game viewport Public float Zoom //Zoom level of our image

Public Vector2 Speed //Speed of movement of our parallax effect Private Vector2 Offset //Offset to start drawing our image Private Texture2D Texture //The image to use
PARALLAX EFFECTS GAME CODE
Have a look at the documented code below: using This class will keep track of individual background images, speed, and zoom level. We create a Background class, so that we can layer multiple backgrounds together. This effect can be accomplished quite easily in Monogame. First, take a look at the demo video to see what we will be doing:
PARALLAX EFFECTS GAME HOW TO
If(CollisionManager.HasCollided = false)//Only scroll if collision has not happened yetĮDIT: Also, whenever you need to restart your game, you need to set CollisionManager.HasCollided to false.This post will explain how to create a parallax effect in Monogame. Now, you should change it to something like this public override void Update()
PARALLAX EFFECTS GAME UPDATE
(Maybe you made it in a different class or renamed it so please understand and apply the changes in that class only)įor the sake of completion, Let's say your said Update function looked like this: public override void Update() I am going to assume your scrolling is done in the Update() method of your default Game1 class. Now, where you have the logic of scrolling, you only scroll if this field is true. Game1.HasCollided = true //This is all you need here. So, you need to change your existing Update() method inside the CollisionManager class as public class CollisionManager : GameComponent The CollisionManger object now checks for collision and sets it to true in case a collision occurs. This field indicates whether the collision has taken place or not. So First, make a public static bool field inside your CollisionManger class, such as public static bool HasCollided=false After this, wherever your scrolling logic is, it should see this bool value and change the scrolling as you desire. I feel a better approach would be to make your CollisionManager to set a bool. However, I think trying to stop the scrolling effect inside this class is not a good idea Not that it is impossible, but your code will only become messy and harder to maintain/understand. I can see that the object CollisionManager is checking the collisions with the Car and obstacle objects, and I don't find any major issues with the said code. I can't see the implementation of parallax effect in your given code. I feel that you are yet to give more details, but anyhow I'll try to answer. Return new Rectangle((int)position2.X, (int)position2.Y, tex.Width, tex.Height) Return new Rectangle((int)position1.X, (int)position1.Y, tex.Width, tex.Height) Return new Rectangle((int)position.X, (int)position.Y, tex.Width, tex.Height) Getbound funcation for car and obstacle public Rectangle getBound() I want do something here, but I can't make getBound funcaiton here, If (obstacleRect1.Intersects(carRect) ||obstacleRect2.Intersects(carRect)) Rectangle obstacleRect2 = obstacle.getBound2()

Rectangle obstacleRect1 = obstacle.getBound1() if the car boundary intersects with the obstacle boundary Public override void Update(GameTime gameTime) Obstacle obstacle, Road road) : base(game) Public CollisionManager(Game game,RacingCar car, Here is my collision manager public class CollisionManager : GameComponent I can't use the same way stop the car to stop the road, because they are not intersect with each other. when the car hits the obstacle, I need the road also stop the scrolling effect. I am doing a small car racing game on mono game, for the road and obstacle I am using parallax scrolling effect.
