Witam, mam problem :d Otóż, robię przewijany background2D i jest taka sprawa, że coś się krzaczy i drugi obrazek się cześciowo nakłada, a zamiast przewijać się automatycznie, gdy idzie tlo1 do góry, na dole robi się niebieskie tło... i gdy niebieskie dojdzie do góry nagle pokazuje się tlo3 i tak cały czas... Tutaj zamieszczam cały kod.
I jeszcz jedno pytanko... Czemu plik release i debug odpala się tylko po wciśnięciu f5..? A jak wchodzę do folderu i odpalam release lub debug to Od razu wychodzi błąd i aplikacja się zamyka..?
Game1.cs
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace Meteory_chaosu
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
//Inicjalizacja klasy Sprite poprzez utworzenie obiektu mSprite
Sprite mSprite;
Sprite mSpriteDrugi;
Sprite mBackgroundPierwszy;
Sprite mBackgroundDrugi;
Sprite mBackgroundTrzeci;
Sprite mBackgroundCzwarty;
Sprite mBackgroundPiąty;
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
mSprite = new Sprite();
mSpriteDrugi = new Sprite();
mBackgroundPierwszy = new Sprite();
mBackgroundPierwszy.Scale = 1.0f;
mBackgroundDrugi = new Sprite();
mBackgroundDrugi.Scale = 1.0f;
mBackgroundTrzeci = new Sprite();
mBackgroundTrzeci.Scale = 1.0f;
mBackgroundCzwarty = new Sprite();
mBackgroundCzwarty.Scale = 1.0f;
mBackgroundPiąty = new Sprite();
mBackgroundPiąty.Scale = 1.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);
// TODO: use this.Content to load your game content here
mSprite.LoadContent(this.Content, "statek");
mSprite.Position = new Vector2(125, 245);
mSpriteDrugi.LoadContent(this.Content, "statek");
mSpriteDrugi.Position = new Vector2(200, 400);
mBackgroundPierwszy.LoadContent(this.Content, "tlo");
mBackgroundPierwszy.Position = new Vector2(0, 0);
mBackgroundDrugi.LoadContent(this.Content, "tlo2");
mBackgroundDrugi.Position = new Vector2(mBackgroundPierwszy.Position.Y + mBackgroundPierwszy.Size.Height, 0);
mBackgroundTrzeci.LoadContent(this.Content, "tlo3");
mBackgroundTrzeci.Position = new Vector2(mBackgroundDrugi.Position.Y + mBackgroundDrugi.Size.Height, 0);
mBackgroundCzwarty.LoadContent(this.Content, "tlo4");
mBackgroundCzwarty.Position = new Vector2(mBackgroundTrzeci.Position.Y + mBackgroundTrzeci.Size.Height, 0);
mBackgroundPiąty.LoadContent(this.Content, "tlo5");
mBackgroundPiąty.Position = new Vector2(mBackgroundCzwarty.Position.Y + mBackgroundCzwarty.Size.Height, 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();
// TODO: Add your update logic here
if (mBackgroundPierwszy.Position.Y < -mBackgroundPierwszy.Size.Height)
{
mBackgroundPierwszy.Position.Y = mBackgroundDrugi.Position.Y + mBackgroundDrugi.Size.Height;
}
if (mBackgroundDrugi.Position.Y < -mBackgroundDrugi.Size.Height)
{
mBackgroundDrugi.Position.Y = mBackgroundTrzeci.Position.Y + mBackgroundTrzeci.Size.Height;
}
if (mBackgroundTrzeci.Position.Y < -mBackgroundTrzeci.Size.Height)
{
mBackgroundTrzeci.Position.Y = mBackgroundCzwarty.Position.Y + mBackgroundCzwarty.Size.Height;
}
if (mBackgroundCzwarty.Position.Y < -mBackgroundCzwarty.Size.Height)
{
mBackgroundCzwarty.Position.Y = mBackgroundPiąty.Position.Y + mBackgroundPiąty.Size.Height;
}
if (mBackgroundPiąty.Position.Y < -mBackgroundPiąty.Size.Height)
{
mBackgroundPiąty.Position.Y = mBackgroundPierwszy.Position.Y + mBackgroundPierwszy.Size.Height;
}
Vector2 aDirection = new Vector2(0, -1);
Vector2 aSpeed = new Vector2(0, 160);
mBackgroundPierwszy.Position += aDirection * aSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
mBackgroundDrugi.Position += aDirection * aSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
mBackgroundTrzeci.Position += aDirection * aSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
mBackgroundCzwarty.Position += aDirection * aSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
mBackgroundPiąty.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();
mBackgroundPierwszy.Draw(this.spriteBatch);
mBackgroundDrugi.Draw(this.spriteBatch);
mBackgroundTrzeci.Draw(this.spriteBatch);
mBackgroundCzwarty.Draw(this.spriteBatch);
mBackgroundPiąty.Draw(this.spriteBatch);
mSprite.Draw(this.spriteBatch);
mSpriteDrugi.Draw(this.spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
Sprite.cs
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 Meteory_chaosu
{
class Sprite
{
//Obecna pozycja obiektu
public Vector2 Position = new Vector2(100, 200);
//Tekstura 2D będzie używana do rysowania obiektu
private Texture2D mSpriteTexture;
//Wczytywana jest tekstura obiektu
public void LoadContent(ContentManager theContentManager, string theAssetName)
{
mSpriteTexture = theContentManager.Load<Texture2D>(theAssetName);
//Obliczanie rozmiaru obrazka z uwzględnieniem Scale
Size = new Rectangle(0, 0, (int)(mSpriteTexture.Width * Scale), (int)(mSpriteTexture.Height * Scale));
}
//Rysuj obiekt na ekranie
public void Draw(SpriteBatch theSpriteBatch)
{
theSpriteBatch.Draw(mSpriteTexture, Position, new Rectangle(0, 0, mSpriteTexture.Width, mSpriteTexture.Height), Color.White, 0.0f, Vector2.Zero, Scale, SpriteEffects.None, 0);
}
//Rozmiar obiektu
public Rectangle Size;
//Używane aby zmniejszyć lub zwiększyć rozmiar oryginalnego obrazka
public float Scale = 1.0f; //1.0 = oryginalny rozmiar
}
}