//Adds a background sprite to be scrolled through the screen
        public void AddBackground(string theAssetName)
        {
            BackGround oBackgroundSprite = new BackGround();
            oBackgroundSprite.AssetName = theAssetName;

            listOfBackgrounds.Add(oBackgroundSprite);
        }
        Viewport viewport; //The viewing area for drawing the Scrolling background images within

        #endregion Fields

        #region Constructors

        //Constructor
        public BackGround(Viewport theViewport)
        {
            listOfBackgrounds = new List<BackGround>();
            rightMostBackground = null;
            leftMostBackground = null;
            viewport = theViewport;
        }
Exemple #3
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here

            //Initialize and add the background images to the Scrolling background.
            backgrounds = new BackGround(this.GraphicsDevice.Viewport);
            backgrounds.AddBackground("Background01");
            backgrounds.AddBackground("Background02");
            backgrounds.AddBackground("Background03");
            backgrounds.AddBackground("Background04");
            backgrounds.AddBackground("Background05");

            //Load the content for the Scrolling background
            backgrounds.LoadContent(this.Content);

            //Load the content for the Player
            player.LoadContent(this.Content);

            //Load the content for the Score
            score = Content.Load<SpriteFont>("Score");
        }
        //Update the positon of the background images
        public void Update(GameTime theGameTime, int theSpeed)
        {
            //Check to see if any of the Background sprites have moved off the screen
            //if they have, then move them to the right of the chain of scrolling backgrounds
            foreach (BackGround oBackgroundSprite in listOfBackgrounds)
            {
                if (oBackgroundSprite.position.X < viewport.X - oBackgroundSprite.size.Width)
                {
                    oBackgroundSprite.position = new Vector2(rightMostBackground.position.X + rightMostBackground.size.Width, viewport.Y);
                    rightMostBackground = oBackgroundSprite;
                }
            }

            //Set the Direction based on movement to the left or right that was passed in (-1 goes right, 1 goes left)
            Vector2 direction = new Vector2(-1, 0);

            foreach (BackGround oBackgroundSprite in listOfBackgrounds)
            {
                oBackgroundSprite.Update(theGameTime, new Vector2(theSpeed, 0), direction);
            }
        }
        public void LoadContent(ContentManager theContentManager)
        {
            //Clear the Sprites currently stored as the left and right ends of the chain
            rightMostBackground = null;
            leftMostBackground = null;

            //The total width of all the sprites in the chain
            float width = 0.0f;

            //Cycle through all of the Background sprites that have been added
            //and load their content and position them.
            foreach (BackGround oBackgroundSprite in listOfBackgrounds)
            {
                //Load the background's content and apply it's scale, the scale is calculated by figuring
                //out how far the sprite needs to be stretech to make it fill the height of the viewport
                oBackgroundSprite.LoadContent(theContentManager, oBackgroundSprite.AssetName);
                oBackgroundSprite.Scale = viewport.Height / oBackgroundSprite.size.Height;

                if (rightMostBackground == null)
                {
                    //Position the first Background sprite in line at the (0,0) position
                    oBackgroundSprite.position = new Vector2(viewport.X, viewport.Y);
                    leftMostBackground = oBackgroundSprite;
                }
                else
                {
                    //Position the sprite after the last sprite in line
                    oBackgroundSprite.position = new Vector2(rightMostBackground.position.X + rightMostBackground.size.Width, viewport.Y);
                }

                //Set the sprite as the last one in line
                rightMostBackground = oBackgroundSprite;

                //Increment the width of all the sprites combined in the chain
                width += oBackgroundSprite.size.Width;

            }

            //If the Width of all the sprites in the chain does not fill the twice the Viewport width
            //then cycle through the images over and over until we have added
            //enough background images to fill the twice the width.
            int index = 0;
            if (listOfBackgrounds.Count > 0 && width < viewport.Width * 2)
            {
                do
                {
                    //Add another background image to the chain
                    BackGround oBackgroundSprite = new BackGround();
                    oBackgroundSprite.AssetName = listOfBackgrounds[index].AssetName;
                    oBackgroundSprite.LoadContent(theContentManager, oBackgroundSprite.AssetName);
                    oBackgroundSprite.Scale = viewport.Height / oBackgroundSprite.size.Height;
                    oBackgroundSprite.position = new Vector2(rightMostBackground.position.X + rightMostBackground.size.Width, viewport.Y);
                    listOfBackgrounds.Add(oBackgroundSprite);
                    rightMostBackground = oBackgroundSprite;

                    //Add the new background Image's width to the total width of the chain
                    width += oBackgroundSprite.size.Width;

                    //Move to the next image in the background images
                    //If we've moved to the end of the indexes, start over
                    index += 1;
                    if (index > listOfBackgrounds.Count - 1)
                    {
                        index = 0;
                    }

                } while (width < viewport.Width * 2);
            }
        }