Quantcast
Channel: Community | MonoGame - Latest topics
Viewing all articles
Browse latest Browse all 6821

Unloading content results in black screen

$
0
0

@Fox9 wrote:

After unloading content, the iPhone simulator is still drawing the texture(a black texture instead of the real texture). Is this normal or is it a bug? Should the screen not be completely blue(Color.CornflowerBlue)?
Or am I doing something wrong?

Before unloading content:

After unloading content:

Game1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace iPhoneGamestates
{
public class Game1 : Game
{
GraphicsDevice graphicsdevice;
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

	public Camera camera;
	public Vector2 vp, gameWorldSize = new Vector2(1334, 750);
	public float ScaleX, ScaleY, Scale;
	Vector2 Newcameraposition;

	public Texture2D BallSprite;

	public Texture2D Menubackground, Introbackground;
	public float delta;
	public bool NextGameState = false;
	IState lastState, currentState, savelastState;

	public ContentManager IntroContent;
	public ContentManager MenuContent;


	public enum GameStates
	{
		IntroState = 0,
		MenuState = 1
	}

	public void ChangeGameState(GameStates newState)
	{
		if (newState == currentGameState)
			return;
		lastGameState = currentGameState;
		lastState = currentState;
		switch (newState)
		{
			case GameStates.IntroState:
				currentState = new Intro(this, GraphicsDevice);
				currentGameState = GameStates.IntroState;
				break;
			case GameStates.MenuState:
				currentState = new Menu(this, GraphicsDevice);
				currentGameState = GameStates.MenuState;
				break;								
		}
		currentState.Load(Content);
	}


	public void ChangeToLastandCurrent()
	{
		savelastGameState = currentGameState;
		currentGameState = lastGameState;
		lastGameState = savelastGameState;
		savelastState = currentState;
		currentState = lastState;
		lastState = savelastState;
	}

	public GameStates CurrentState
	{
		get { return currentGameState; }
		set { currentGameState = value; }
	}

	public GameStates LastState
	{
		get { return lastGameState; }
		set { lastGameState = value; }
	}

	private GameStates currentGameState;
	private GameStates lastGameState;
	private GameStates savelastGameState;


	public Game1()
	{
		graphics = new GraphicsDeviceManager(this);
		Content.RootDirectory = "Content";
		graphics.IsFullScreen = true;
	}


	protected override void Initialize()
	{
		currentState = new Intro(this, graphicsdevice);
		currentGameState = GameStates.IntroState;

		base.Initialize();
	}

	protected override void UnloadContent()
	{
		Content.Unload();
	}

	public void IntroLoad()
	{
		Introbackground = IntroContent.Load<Texture2D>("Content/Introbackground");
	}



	public void MenuLoad()
	{
		Menubackground = MenuContent.Load<Texture2D>("Content/Menubackground");
	}

	protected override void LoadContent()
	{
		spriteBatch = new SpriteBatch(GraphicsDevice);
		vp = new Vector2(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);

		ScaleX = vp.X / gameWorldSize.X;
		ScaleY = vp.Y / gameWorldSize.Y;
		Scale = Math.Min(ScaleX, ScaleY);

		camera = new Camera(new Vector2(gameWorldSize.X / 2, gameWorldSize.Y / 2), Scale, vp);

		currentState.Load(Content);

		TouchPanel.EnabledGestures = GestureType.Tap;
	}


	protected override void Update(GameTime gameTime)
	{
		delta = (float)gameTime.ElapsedGameTime.TotalSeconds;

		Newcameraposition = new Vector2(gameWorldSize.X / 2, gameWorldSize.Y / 2);

		camera.Update(gameTime, Newcameraposition, Scale, vp);


		while (TouchPanel.IsGestureAvailable)
		{
			GestureSample gs = TouchPanel.ReadGesture();
			switch (gs.GestureType)
			{
				case GestureType.Tap:						
					NextGameState = true;
				break;
			}
		}

		currentState.Update(gameTime);

		base.Update(gameTime);
	}


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

		spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, camera.GetMatrix());		  	
		  currentState.Render(spriteBatch);
		spriteBatch.End();


		base.Draw(gameTime);
	}
}

}

Intro.cs

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.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace iPhoneGamestates
{
public class Intro : IState
{
private Game1 game1;
GraphicsDevice graphicsDevice;
float Count = 5f;
bool ChangeGameState = true;

	public Intro(Game1 game, GraphicsDevice device)
	{
		game1 = game;
		graphicsDevice = device;
		game1.IntroContent = new ContentManager(game1.Services);
	}

	public void Load(ContentManager content)
	{
		game1.IntroLoad();
	}

	public void Update(GameTime gametime)
	{
		Count -= game1.delta;
		if ((Count < 0) && (ChangeGameState == true))
		{
			ChangeGameState = false;
			if (game1.IntroContent != null)
				game1.IntroContent.Unload();
		}
	}
	public void Render(SpriteBatch batch)
	{
		batch.Draw(game1.Introbackground, new Rectangle((int)game1.gameWorldSize.X / 2, (int)game1.gameWorldSize.Y / 2, game1.Introbackground.Width, game1.Introbackground.Height), null, Color.White, 0, new Vector2(game1.Introbackground.Width / 2, game1.Introbackground.Height / 2), SpriteEffects.None, 0.10f);
	}
}

}

Posts: 2

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 6821

Trending Articles