Thursday, March 8, 2012

Parallaxing backgrounds!!!

So yeah, parallaxing backgrounds are pretty nifty! Nice little trick to make a flat 2d game have some feel of depth. Plus it can also be funny to set the different parts of the background to ridiculous speeds. Basically you take your nice background and split it up into various different elements/layers. So a layer for the foreground, clouds, etc, etc. Speaking of backgrounds I've also finished all the different level's backgrounds.

I was actually surprised how simple it was to implement the parallaxing background. I thought there would have been some special function, but nope just creating multiple background variables.

CODE:

// Parallaxing Layers
Background bgLayer1;
Background bgLayer2;
Background bgLayer3;
...
//Initializing the backgrounds
protected override void Initialize()
{
PositionMenu.X = 0;
PositionMenu.Y = 0;
// TODO: Add your initialization logic here
bgLayer1 = new Background();
bgLayer2 = new Background();
bgLayer3 = new Background();
game = false;

base.Initialize();
}

...
//Loading in the files into the game world used for the backgrounds
protected override void LoadContent()
{
device = graphics.GraphicsDevice;
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
screenWidth = device.PresentationParameters.BackBufferWidth;
screenWidth = device.PresentationParameters.BackBufferWidth;

mainmenu = Content.Load("mainmenu");
dashboard = Content.Load("dashboard");
// Load the parallaxing background

bgLayer3.Initialize(Content, "bglayer2lvl1", GraphicsDevice.Viewport.Width, -2);
bgLayer2.Initialize(Content, "bglayer1lvl1", GraphicsDevice.Viewport.Width, -1);
bgLayer1.Initialize(Content, "mainbackgroundlvl1", GraphicsDevice.Viewport.Width, 0);

...

//Then ya need to actually draw and update them :D


I don't know why I thought it would be more complex than just putting in more background variables, but sure it ended up being just that simple. Despite being straight it does actually look rather nice. Here is a video of it running. Graphically I think the game is coming along nicely, still have to figure out how to get the sprite sheet working to animate the players.



No comments:

Post a Comment