PDA

View Full Version : Background su XNA Game Studio


HipT
13-10-2007, 17:12
Ciao a tutti.
Vorrei sapere come posso inserire un background su un programma con xna game studio.Ora,riesco a visualizzare una texture ma devo visualizzare un background,
ho cercato alcuni esempi in internet,provato a inserire il codice nel mio programma ma mi da' degli errori...

Vi riporto il codice:

#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion

public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
ContentManager content;
//The 2D textures for the background
Texture2D[] mBackgroundTexture = new Texture2D[1];

//The X position of the background
int mBackground1X = 0;


//The object to draw the sprites
SpriteBatch mSpriteBatch;

public Game1()
{
graphics = new GraphicsDeviceManager(this);
content = new ContentManager(Services);
////////////////////////////////
}

protected override void Initialize()
{
base.Initialize();
}

// This is a texture we can render.
Texture2D sfondo,button1;

// Set the coordinates to draw the sprite at.
Vector2 posfondo = Vector2.Zero;
Vector2 posbutton1 = Vector2.Zero;

// This is the object that will draw the sprites.
SpriteBatch spriteBatch;
SpriteBatch imgbutton1Batch;


protected override void LoadGraphicsContent(bool loadAllContent)
{
if (loadAllContent)
{
//mette a modalità finestra e imposta la risoluzione
graphics.IsFullScreen = false;
graphics.PreferredBackBufferHeight = 768;
graphics.PreferredBackBufferWidth = 1024;
graphics.ApplyChanges();
/////////////////////
sfondo = content.Load<Texture2D>("416");
spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
///////////////
imgbutton1Batch = new SpriteBatch(graphics.GraphicsDevice);
button1 = content.Load<Texture2D>("entry");
}
}
void LoadResources()
{
//Load the image into the texture object
ContentManager aLoader = new ContentManager(Services);

//Load the image into the texture object
mBackgroundTexture[0] = aLoader.Load<Texture2D>("Background1") as Texture2D;

//Set the starting "X" position for each of the background
mBackground1X = 0;
//Initialze the sprite batch object. This will be used to draw the sprite
mSpriteBatch = new SpriteBatch(graphics.GraphicsDevice);
}

protected override void UnloadGraphicsContent(bool unloadAllContent)
{
if (unloadAllContent == true)
{
content.Unload();
}
}

// Store some information about the sprite's motion.
Vector2 spriteSpeed = new Vector2(50.0f, 50.0f);

protected override void Update(GameTime gameTime)
{
// Allows the default game to exit on Xbox 360 and Windows.
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
base.Update(gameTime);
}

void UpdateSprite1(GameTime gameTime)
{

}

protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

// Draw the sprite.
spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
spriteBatch.Draw(sfondo, posfondo, Color.White);
spriteBatch.End();
///////////////
mSpriteBatch.Begin(SpriteBlendMode.Additive);
mSpriteBatch.Draw(mBackgroundTexture[0], new Rectangle(mBackground1X, 0, this.Window.ClientBounds.Width, this.Window.ClientBounds.Height), Color.White);
mSpriteBatch.End();
///////////////

base.Draw(gameTime);
}
}


L'errore me lo da' nel metodo "Draw",nell'ultima parte,quella compresa tra "/"
In debug non mi da' errori,solo in esecuzione si chiude tutto e mi segna la riga
"mSpriteBatch.Begin(SpriteBlendMode.Additive);" e se tolgo quella,le altre sotto...l'errore:"Riferimento a un oggetto non impostato su un'istanza di oggetto."

Come posso fare?
Questa cosa mi blocca di brutto...:muro: :muro: :muro:

Grazie.