Sign in to follow this  

Little bigger problem than before, C#

This topic is 2337 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello again.. I've been following the tutorial i found on this website about how to make a scrolling background with C# XNA
[url="http://www.xnadevelopment.com/tutorials/scrollinga2dbackground/ScrollingA2DBackground.shtml"]http://www.xnadevelopment.com/tutorials/scrollinga2dbackground/ScrollingA2DBackground.shtml[/url]

Now the problem is when i start the application i see the scrolling background but i only see the first picture scrolling by and after that no pictures..
Here is the 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;

namespace oskarGame
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

World world;

World myBackgroundOne;
World myBackgroundTwo;
World myBackgroundThree;
World myBackgroundFour;
World myBackgroundFive;



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()
{
world = new World();

myBackgroundOne = new World();
myBackgroundOne.Scale = 2.0f;

myBackgroundTwo = new World();
myBackgroundTwo.Scale = 2.0f;

myBackgroundThree = new World();
myBackgroundThree.Scale = 2.0f;

myBackgroundFour = new World();
myBackgroundFour.Scale = 2.0f;

myBackgroundFive = new World();
myBackgroundFive.Scale = 2.0f;

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

myBackgroundOne.LoadContent(this.Content, "worldBackground/Background01");
myBackgroundOne.Position = new Vector2(0, 0);

myBackgroundTwo.LoadContent(this.Content, "worldBackground/Background02");
myBackgroundTwo.Position = new Vector2(myBackgroundOne.Position.X + myBackgroundOne.size.Width, 0);

myBackgroundThree.LoadContent(this.Content, "worldBackground/Background03");
myBackgroundThree.Position = new Vector2(myBackgroundTwo.Position.X + myBackgroundTwo.size.Width, 0);

myBackgroundFour.LoadContent(this.Content, "worldBackground/Background04");
myBackgroundFour.Position = new Vector2(myBackgroundThree.Position.X + myBackgroundThree.size.Width, 0);

myBackgroundFive.LoadContent(this.Content, "worldBackground/Background05");
myBackgroundFive.Position = new Vector2(myBackgroundFour.Position.X + myBackgroundFour.size.Width, 0);
}

/// <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();


if(myBackgroundOne.Position.X < -myBackgroundOne.size.Width)
{
myBackgroundOne.Position.X = myBackgroundFive.Position.X + myBackgroundFive.size.Width;
}

if (myBackgroundTwo.Position.X < -myBackgroundTwo.size.Width)
{
myBackgroundTwo.Position.X = myBackgroundOne.Position.X + myBackgroundOne.size.Width;
}

if (myBackgroundThree.Position.X < -myBackgroundThree.size.Width)
{
myBackgroundThree.Position.X = myBackgroundTwo.Position.X + myBackgroundTwo.size.Width;
}

if (myBackgroundFour.Position.X < -myBackgroundFour.size.Width)
{
myBackgroundFour.Position.X = myBackgroundThree.Position.X + myBackgroundThree.size.Width;
}

if (myBackgroundFive.Position.X < -myBackgroundFive.size.Width)
{
myBackgroundFive.Position.X = myBackgroundFour.Position.X + myBackgroundFour.size.Width;
}

Vector2 aDirection = new Vector2(-1, 0);
Vector2 aSpeed = new Vector2(160, 0);

myBackgroundOne.Position += aDirection * aSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
myBackgroundTwo.Position += aDirection * aSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
myBackgroundThree.Position += aDirection * aSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
myBackgroundFour.Position += aDirection * aSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
myBackgroundFive.Position += aDirection * aSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;

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
spriteBatch.Begin();
myBackgroundOne.Draw(this.spriteBatch);
myBackgroundTwo.Draw(this.spriteBatch);
myBackgroundThree.Draw(this.spriteBatch);
myBackgroundFour.Draw(this.spriteBatch);
myBackgroundFive.Draw(this.spriteBatch);
spriteBatch.End();

base.Draw(gameTime);
}
}
}
[/code]

And here is the World class:
[code]using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace oskarGame
{
class World
{
//The size of the BackGround
public Rectangle size;

//Used to size the BackGround up and down from the original image
public float Scale = 1.0f;

//The Background texture variable
private Texture2D myWorldTexture;

//Controls the position of the background
public Vector2 Position = new Vector2(0, 0);

//Load the texture for the background using content pipeline
public void LoadContent(ContentManager myContentManager, string AssetName)
{
myWorldTexture = myContentManager.Load<Texture2D>(AssetName);
}

//Draw the background on the screen
public void Draw(SpriteBatch mySpriteBatch)
{
mySpriteBatch.Draw(myWorldTexture, Position,
new Rectangle(0, 0, myWorldTexture.Width, myWorldTexture.Height),
Color.White, 0.0f, Vector2.Zero, Scale, SpriteEffects.None, 0);
}
}
}
[/code]

I have been looking through the code and double checked the code from the tutorial but i don't see what is the error here.

Would be greatful if i could get some help.

Share this post


Link to post
Share on other sites
You never initialize your size variable.

[quote]
Size = new Rectangle(0, 0, (int)(mSpriteTexture.Width * Scale), (int)(mSpriteTexture.Height * Scale));
[/quote]

Share this post


Link to post
Share on other sites
[quote name='TheCompBoy' timestamp='1311184418' post='4838064']
I do initialize my size variable in the World class, Please i don't understand..
[/quote]

Yeah in the article you posted they initial the Size variable (I believe you represent it as the Size in your world class) and than subsequently it is used throughout to determine how the background scrolls (from what I am reading). So you need to initialize your Size variable because right now you are referencing it but it is initialized to new Rectangle(0,0,0,0) by default where as it needs to be new Rectangle(0,0, Texture.Width * Scale, Texture.Height * Scale).

**EDIT*** Sorry I misread your response but I think what I said still applies. I do not see in the code snippet you pasted an initialization of the Size variable. I see it declared (taking the default value) but not explicitly initialized. Edited by XXChester

Share this post


Link to post
Share on other sites
Alright i understand.. But where should i initialize it? In World class or in Game1 class ?

EDIT: Thanks i found it myself! Realy thanks for the help i gave you both + rep!!
My english is not the best but i mixed Declare variable and initialize variable.. My bad..

Share this post


Link to post
Share on other sites
[quote name='TheCompBoy' timestamp='1311185209' post='4838073']
Alright i understand.. But where should i initialize it? In World class or in Game1 class ?

EDIT: Thanks i found it myself! Realy thanks for the help i gave you both + rep!!
My english is not the best but i mixed Declare variable and initialize variable.. My bad..
[/quote]

Glad you figured it out and I assumed that you were mixing them up that is why I stated it clearly what you need to do. Best of luck with your future development.

Share this post


Link to post
Share on other sites

This topic is 2337 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this