I am trying to encapsulate my game objects by expanding Mircosoft.Xna.Framework.GameCompenent, and then just creating them and managing them in the Update () method of Game1. I have a Game1 class, a Player class, and an animation class. The animation is supposed to control the changes to the Texture2D object, in this case Player.
My problem is that although I have successfully expanded everything, I have no syntax errors, I didn’t select any exceptions, and checked and re-checked what small code I wrote, the override functions are not called, and I get a black screen.
Game1.cs: (note that only two lines are changed for the Player declaration)
public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; Player player; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } protected override void Initialize() { player = new Player(this); base.Initialize(); } protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); } protected override void UnloadContent() { } protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit();
Player.cs:
class Player : Microsoft.Xna.Framework.DrawableGameComponent { Rectangle bounds; Texture2D t; Animation[] animations = new Animation[4]; String path = @"..\..\..\Content\player.png"; #region Animation Constants private const int WALK_RIGHT = 0; #endregion SpriteBatch spriteBatch; public Player(Game game) : base(game) {
Animation.cs:
class Animation : Microsoft.Xna.Framework.GameComponent { Game game; String name; //name of default sprite; standing, unmoving, neutral, etc. The rest of the animation sprite names should derive from this String keyword; int frameCount; int delay; //frames between texture change String[] paths; //texture pathnames generated by the MakePaths() function int currentFrame = 0; int delayCount = 0; bool playing = false; public Animation(Game associatedGame, String nameVal, String keywordVal, int frameCountVal) : base(associatedGame) { name = nameVal; keyword = keywordVal; frameCount = frameCountVal; paths = MakePaths(); delay = 10; } public Animation(Game associatedGame, String nameVal, String keywordVal, int frameCountVal, int delayVal) : base(associatedGame) { name = nameVal; keyword = keywordVal; frameCount = frameCountVal; paths = MakePaths(); delay = delayVal; } private String[] MakePaths() { //format: name_keyword_anim[i] //format example: player_walking_anim1 String[] temp = new String[frameCount]; for (int i = 0; i < frameCount; i++) { temp[i] = name + "_" + keyword + "_" + "anim" + i.ToString(); } return temp; } public Texture2D getTexture() { return Game.Content.Load<Texture2D>(paths[currentFrame]); } public void Play() { playing = true; } public void Stop() { currentFrame = 0; delayCount = 0; playing = false; } public bool Playing { get { return playing; } } public override void Update(GameTime gameTime) { if (playing) { if (delayCount == delay) { delayCount = 0; if ((currentFrame + 1) == frameCount) currentFrame = 0; else currentFrame++; } else delayCount++; } base.Update(gameTime); } public override string ToString() { return "params: " + name + "," + keyword + "," + frameCount.ToString() + "\nUsing paths: " + paths; } }
The only calls to LoadContent, Initialize, Update, and Draw are those specified in Game1. What really puzzles me is that I was able to use this technique before, without any problems. These features will naturally be triggered by the Xna update process.
So ... why is this?