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);
}
}
}