Hi, I'm currently building a
simple project to learn XNA and the way it's designed to be used.
So, I used the GameState tutorial, add my model and behaviours, and a simple DrawableGameComponent component to draw current FPS rate.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | #region Using Statements using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; #endregion
namespace AITC2 { /// <summary> /// This is A game component that is notified when it needs to draw itself. /// It implements IDrawable. /// </summary> public partial class StatisticComponent : Microsoft.Xna.Framework.DrawableGameComponent { #region Fields // To deals with content loading ContentManager content;
// Text management SpriteBatch spriteBatch; SpriteFont font1; Vector2 textPosition;
int frameRate = 0; int frameCounter = 0; TimeSpan elapsedTime = TimeSpan.Zero;
#endregion
public StatisticComponent(Game game, ContentManager content) : base(game) { // TODO: Construct any child components here this.content = content; }
/// <summary> /// Allows the game component to perform any initialization it needs to before starting /// to run. This is where it can query for any required services and load content. /// </summary> public override void Initialize() { // TODO: Add your initialization code here //Text position textPosition = new Vector2(0, 0); base.Initialize(); }
/// <summary> /// Load graphics content specific to your component. /// </summary> protected override void LoadContent() { font1 = content.Load<SpriteFont>("Fonts\\Pescab"); // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(this.Game.GraphicsDevice); }
/// <summary> /// Unload graphics content specific to your drawable component. /// </summary> protected override void UnloadContent() {
}
/// <summary> /// Allows the component 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> public override void Update(GameTime gameTime) {
//FPS Stuff elapsedTime += gameTime.ElapsedGameTime;
if (elapsedTime > TimeSpan.FromSeconds(1)) { elapsedTime -= TimeSpan.FromSeconds(1); frameRate = frameCounter; frameCounter = 0; }
base.Update(gameTime); }
/// <summary> /// This is called when the component should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> public override void Draw(GameTime gameTime) {
spriteBatch.Begin();
frameCounter++; //format the stging string output = string.Format("fps: {0}", frameRate);
// Draw the string spriteBatch.DrawString(font1, output, textPosition, Color.LightGreen);
spriteBatch.End();
base.Draw(gameTime); } } } |
When I quit my GameScreen (where the component is added), I get a "Cannot access a disposed object. Object name: 'Texture2D'." exception (line 116).
It's the only component that give such a result. Others (like FireParticleSystem from work fine.
I found a hack (removing the component before quiting) but it's not the clean way.
Any idea ?