Need help with XNA game in c#

This is a discussion on Need help with XNA game in c# within the Game Programming forums, part of the General Programming Boards category; Basically no charecter functionality is made in the game, I have a side scrolling background adn then platforms that move ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    35

    Need help with XNA game in c#

    Basically no charecter functionality is made in the game, I have a side scrolling background adn then platforms that move automatically to the left, basically the first background is working correctly but the platforms once they move past the screen bounary and appear again instead of going in order they overlap each other :S! (dont copy the name spaces) they may be differnet from yours, Also please download the textures and add them to the content folder so you can see the images otherwise you cant see anything or youll get eerrosPlease look at my program:

    Game1 class:
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Audio;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.GamerServices;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;
    using Microsoft.Xna.Framework.Media;
    using ParallaxScrolling;
    
    namespace WindowsGame3
    {
        /// <summary>
        /// This is the main type for your game
        /// </summary>
        public class Game1 : Microsoft.Xna.Framework.Game
        {
            GraphicsDeviceManager graphics;
            SpriteBatch spriteBatch;
            
            ScrollingBackground backgroundA;
            Building buildingOne, buildingTwo, buildingThree;
    
    
            public Game1()
            {
                graphics = new GraphicsDeviceManager(this);
                Content.RootDirectory = "Content";
            }
    
            /// <summary>
            /// Allows the game to perform any initialization it needs to before starting to run.
            /// This is where it can query for any required services and load any non-graphic
            /// related content.  Calling base.Initialize will enumerate through any components
            /// and initialize them as well.
            /// </summary>
            protected override void Initialize()
            {
                // TODO: Add your initialization logic here
    
                base.Initialize();
            }
    
            /// <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
                 Vector2 stage = new Vector2(graphics.PreferredBackBufferWidth,
                    graphics.PreferredBackBufferHeight);
    
                Texture2D tex = this.Content.Load<Texture2D>("background");
                Texture2D tex2 = this.Content.Load<Texture2D>("platform1");
                Texture2D tex3 = this.Content.Load<Texture2D>("platform2");
                Texture2D tex4 = this.Content.Load<Texture2D>("platform3");
                Rectangle srcRect = new Rectangle(0, 770, tex.Width, 254); 
                Rectangle srcRect2 = new Rectangle(0, 0,379,189);
                Rectangle srcRect3 = new Rectangle(0, 0, 377, 130);
                Rectangle srcRect4 = new Rectangle(0, 0, 179, 245);
                Vector2 positionA = new Vector2(0, stage.Y - srcRect.Height - 200);
                backgroundA = new ScrollingBackground(this, spriteBatch, tex,
                  srcRect, positionA, new Vector2(1, 0), stage);
    
                this.Components.Add(backgroundA);
    
                Vector2 positionB = new Vector2(0, stage.Y - srcRect.Height);
                buildingOne = new Building(this, spriteBatch, tex2, srcRect2, positionB, new Vector2(2, 0), stage);
                this.Components.Add(buildingOne);
    
                Vector2 positionC = new Vector2(400, stage.Y - srcRect2.Height);
                buildingTwo = new Building(this, spriteBatch, tex3, srcRect3, positionC, new Vector2(2, 0), stage);
                this.Components.Add(buildingTwo);
    
                Vector2 positionD = new Vector2(800, stage.Y -srcRect.Height);
                buildingThree = new Building(this, spriteBatch, tex4, srcRect4, positionD, new Vector2(2, 0), stage);
                this.Components.Add(buildingThree);
    
               
    
            }
    
            /// <summary>
            /// UnloadContent will be called once per game and is the place to unload
            /// all content.
            /// </summary>
            protected override void UnloadContent()
            {
                // TODO: Unload any non ContentManager content here
            }
    
            /// <summary>
            /// Allows the game to run logic such as updating the world,
            /// checking for collisions, gathering input, and playing audio.
            /// </summary>
            /// <param name="gameTime">Provides a snapshot of timing values.</param>
            protected override void Update(GameTime gameTime)
            {
                // Allows the game to exit
                if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                    this.Exit();
    
                // TODO: Add your update logic here
    
                base.Update(gameTime);
            }
    
            /// <summary>
            /// This is called when the game should draw itself.
            /// </summary>
            /// <param name="gameTime">Provides a snapshot of timing values.</param>
            protected override void Draw(GameTime gameTime)
            {
                GraphicsDevice.Clear(Color.CornflowerBlue);
    
                // TODO: Add your drawing code here
    
                base.Draw(gameTime);
            }
        }
    }
    building Class:
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Audio;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.GamerServices;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;
    using Microsoft.Xna.Framework.Media;
    
    
    namespace WindowsGame3
    {
        /// <summary>
        /// This is a game component that implements IUpdateable.
        /// </summary>
        public class Building : Microsoft.Xna.Framework.DrawableGameComponent
        {
            private SpriteBatch spriteBatch;
            private Texture2D texture;
            private Rectangle srcRect;
            private Vector2 position;
            private Vector2 speed;
            private Vector2 stage;
            bool isActive = false;
    
            private Vector2 position1, position2;
    
            public Building(Game game, SpriteBatch spriteBatch, Texture2D texture, Rectangle srcRect, Vector2 position, Vector2 speed,Vector2 stage)
                : base(game)
            {
                this.spriteBatch = spriteBatch;
                this.texture = texture;
                this.position = position;
                this.srcRect = srcRect;
                this.speed = speed;
                this.stage = stage;
    
                position1 = new Vector2(position.X, position.Y);
                position2 = new Vector2(texture.Width, position.Y);
                // TODO: Construct any child components here
            }
    
            /// <summary>
            /// Allows the game component to perform any initialization it needs to before starting
            /// to run.  This is where it can query for any required services and load content.
            /// </summary>
            public override void Initialize()
            {
                // TODO: Add your initialization code here
                
                base.Initialize();
            }
            /// <summary>
            /// Allows the game component to update itself.
            /// </summary>
            /// <param name="gameTime">Provides a snapshot of timing values.</param>
            public override void Update(GameTime gameTime)
            {
                // TODO: Add your update code here
                if (position1.X > -texture.Width)
                {
                    position1.X -= speed.X;
                }
                else
                {
                    
                    position1.X = position2.X + texture.Width;
                        
                    
                }
    
                if (position2.X > -texture.Width)
                {
                    position2.X -= speed.X;
                }
                else
                {
                   
                     position2.X = position1.X + texture.Width;
                   
                }
                base.Update(gameTime);
            }
            public override void Draw(GameTime gameTime)
            {
                spriteBatch.Begin();
                spriteBatch.Draw(texture, position1, srcRect, Color.White);
                spriteBatch.End();
                base.Draw(gameTime);
            }
        }
    }
    scrolling background class:
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Audio;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.GamerServices;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;
    using Microsoft.Xna.Framework.Media;
    
    
    namespace ParallaxScrolling
    {
        /// <summary>
        /// This is a game component that implements IUpdateable.
        /// </summary>
        public class ScrollingBackground : Microsoft.Xna.Framework.DrawableGameComponent
        {
            private SpriteBatch spriteBatch;
            private Texture2D tex;
            private Rectangle srcRect;
            private Vector2 position;
            private Vector2 speed;
            private Vector2 stage;
    
            private Vector2 position1, position2;
    
    
            public ScrollingBackground(Game game,
                SpriteBatch spriteBatch,
                Texture2D tex,
                Rectangle srcRect,
                Vector2 position,
                Vector2 speed,
                Vector2 stage)
                : base(game)
            {
                // TODO: Construct any child components here
                this.spriteBatch = spriteBatch;
                this.tex = tex;
                this.srcRect = srcRect;
                this.position = position;
                this.speed = speed;
                this.stage = stage;
                position1 = new Vector2(position.X, position.Y);
                position2 = new Vector2(tex.Width, position.Y);
            }
    
            /// <summary>
            /// Allows the game component to perform any initialization it needs to before starting
            /// to run.  This is where it can query for any required services and load content.
            /// </summary>
            public override void Initialize()
            {
                // TODO: Add your initialization code here
    
                base.Initialize();
            }
    
            /// <summary>
            /// Allows the game component to update itself.
            /// </summary>
            /// <param name="gameTime">Provides a snapshot of timing values.</param>
            public override void Update(GameTime gameTime)
            {
                // TODO: Add your update code here
                if (position1.X > -tex.Width )
                {
                    position1.X -= speed.X;
                }
                else
                {
                    position1.X = position2.X + tex.Width;
                }
    
                if (position2.X > -tex.Width )
                {
                    position2.X -= speed.X;
                }
                else
                {
                    position2.X = position1.X + tex.Width;
                }
    
    
    
                base.Update(gameTime);
            }
    
            public override void Draw(GameTime gameTime)
            {
                spriteBatch.Begin();
                spriteBatch.Draw(tex, position1, srcRect, Color.White);
                spriteBatch.Draw(tex, position2, srcRect, Color.White);
                spriteBatch.End();
    
    
                base.Draw(gameTime);
            }
        }
    }
    Attached Images Attached Images     

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    After a quick read, I think the issue is your code to move the building after it disappears off the screen (ie when position1.X <= -tex.Width )

    Try setting your buildings to all be the same width (ie use same texture for all 3), does the problem will go away?
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    I believe he is failing to account for the fact that quadrilateral positions in 3D refer the the center of the quad. This means that a building is not off the left side of the screen until x - (width / 2) < left_side and not off the right side until x + (width / 2) > right_side. So he is moving the objects back to the other side of the screen at the wrong time which is causing the buildings to overlap by half the width each time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 05-18-2010, 09:42 PM
  2. Should I use a game engine in making a RPG game?
    By m3rk in forum Game Programming
    Replies: 6
    Last Post: 01-26-2009, 03:58 AM
  3. Guessing game: how to quit the game?
    By hzr in forum C Programming
    Replies: 5
    Last Post: 12-18-2008, 09:53 AM
  4. craps game & dice game..
    By cgurl05 in forum C Programming
    Replies: 3
    Last Post: 03-25-2006, 06:58 PM
  5. Game Designer vs Game Programmer
    By the dead tree in forum Game Programming
    Replies: 8
    Last Post: 04-28-2005, 09:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21