I'm doing a college assignment in XNA to make a virtual piano. I have 2 classes, Piano ( my base class) and PianoNotes ( mysubclass)
I have values declared in Piano,which are used in PianoNotes ( inheritance ) and override functions that use polymorphism.
However when I call the draw function in my Game1 file ( equivalent of main I believe), nothing appears. Can someone tell me where I am going wrong?
Here is my code - Game 1 File
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.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace PianoAttempt1
{
/// <summary>
/// This is a piano , using pre-recorded wav files
/// It will have 88 notes
///
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
// Used to draw sprites (2D bitmaps)
public SpriteBatch spriteBatch;
// Instance is a STATIC member, meaning that there is only one of them
// No matter how many instances are created
public static Game1 Instance;
public List<Piano> children = new List<Piano>();
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
// Set back buffer resolution
graphics.PreferredBackBufferWidth = 820;
graphics.PreferredBackBufferHeight = 640;
Instance = this; // Creates an instance of the piano class
}
/// <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
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);
// call a "new" version of piano notes?
children.Add(new Piano());
for (int i = 0; i < children.Count(); i++)
{
children[i].LoadContent();
}
}
/// <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>
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
///
protected override void Update(GameTime gameTime)
{
KeyboardState keyState = Keyboard.GetState();
if (keyState.IsKeyDown(Keys.Escape))
{
this.Exit();
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
//GraphicsDevice.Clear(Color.Green); // This works meaning there is something wrong with the draw function...
// Start drawing sprites, front to back
spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.Opaque);
// Tell all the objects in the scene to draw themselves
for (int i = 0; i < children.Count(); i++)
{
children[i].Draw(gameTime);
}
spriteBatch.End();
base.Draw(gameTime);
}
}
}
Piano File
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.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace PianoAttempt1
{
public class Piano // Base class of all the other classes
{
protected Texture2D Sprite; // Piano bitmap
protected SoundEffect Sound1; // Oct1NoteC
protected SoundEffect Sound2; // Oct1NoteC#
protected SoundEffect Sound3; // Oct1NoteD
protected SoundEffect Sound4; // Oct1NoteD#
protected SoundEffect Sound5; // Oct1NoteE
protected SoundEffect Sound6; // Oct1NoteF
protected SoundEffect Sound7; // Oct1NoteF#
protected SoundEffect Sound8; // Oct1NoteG
protected SoundEffect Sound9; // Oct1NoteG#
protected SoundEffect Sound10; // Oct1NoteA
protected SoundEffect Sound11; // Oct1NoteA#
protected SoundEffect Sound12; // Oct1NoteB
protected SoundEffect Sound13; // Oct2NoteC
protected SoundEffect Sound14; // Oct2NoteC#
protected SoundEffect Sound15; // Oct2NoteD
protected SoundEffect Sound16; // Oct2NoteD#
protected SoundEffect Sound17; // Oct2NoteE
protected SoundEffect Sound18; // Oct2NoteF
protected SoundEffect Sound19; // Oct2NoteF#
protected SoundEffect Sound20; // Oct2NoteG
protected SoundEffect Sound21; // Oct2NoteG
protected SoundEffect Sound22; // Oct2NoteG
protected SoundEffect Sound23; // Oct2NoteG
protected SoundEffect Sound24; // Oct2NoteG#
protected SoundEffect Sound25; // Oct2NoteA
protected SoundEffect Sound26; // Oct2NoteA#
protected SoundEffect Sound27; // Oct2NoteB
public Vector2 Position; // Position of the piano sprite
public Vector2 Look;
public Vector2 Center;
public float Rotation;
public virtual void LoadContent()
{
}
public virtual void Update(GameTime gameTime)
{
}
public virtual void Draw(GameTime gameTime)
{
}
}
}
Piano Notes File
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.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace PianoAttempt1
{
class PianoNotes : Piano // Extends Piano , it is a subclass of Piano
{
public override void LoadContent()
{
// Load the audio files from the content pipeline. No need to include file type
//-------------- Piano Bitmap ----------------------------------
Position.X = 100;
Position.Y = 5;
Sprite = Game1.Instance.Content.Load<Texture2D>("piano_sample");
Center.X = Sprite.Width / 2;
Center.Y = Sprite.Height / 2;
Rotation = 0;
//------------------------------------------------------------
Sound1 = Game1.Instance.Content.Load<SoundEffect>("Oct1NoteC");
//Sound2 = Game1.Instance.Content.Load<SoundEffect>("Oct1NoteC#");
//Sound = Game1.Instance.Content.Load<SoundEffect>("Oct1NoteD");
//Sound = Game1.Instance.Content.Load<SoundEffect>("Oct1NoteD#");
//Sound = Game1.Instance.Content.Load<SoundEffect>("Oct1NoteE");
//Sound = Game1.Instance.Content.Load<SoundEffect>("Oct1NoteF");
//Sound = Game1.Instance.Content.Load<SoundEffect>("Oct1NoteF#");
//Sound = Game1.Instance.Content.Load<SoundEffect>("Oct1NoteG");
//Sound = Game1.Instance.Content.Load<SoundEffect>("Oct1NoteG#");
//Sound = Game1.Instance.Content.Load<SoundEffect>("Oct1NoteA");
//Sound = Game1.Instance.Content.Load<SoundEffect>("Oct1NoteA#");
//Sound = Game1.Instance.Content.Load<SoundEffect>("Oct1NoteB");
//------------ Octave 2 -----------------------
//Sound = Game1.Instance.Content.Load<SoundEffect>("Oct2NoteC");
//Sound = Game1.Instance.Content.Load<SoundEffect>("Oct2NoteC#");
//Sound = Game1.Instance.Content.Load<SoundEffect>("Oct2NoteD");
//Sound = Game1.Instance.Content.Load<SoundEffect>("Oct2NoteD#");
//Sound = Game1.Instance.Content.Load<SoundEffect>("Oct2NoteE");
//Sound = Game1.Instance.Content.Load<SoundEffect>("Oct2NoteF");
//Sound = Game1.Instance.Content.Load<SoundEffect>("Oct2NoteF#");
//Sound = Game1.Instance.Content.Load<SoundEffect>("Oct2NoteG");
//Sound = Game1.Instance.Content.Load<SoundEffect>("Oct2NoteG#");
//Sound = Game1.Instance.Content.Load<SoundEffect>("Oct2NoteA");
//Sound = Game1.Instance.Content.Load<SoundEffect>("Oct2NoteA#");
//Sound = Game1.Instance.Content.Load<SoundEffect>("Oct2NoteB");
base.LoadContent();
}
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
// Gets the state of the keyboard , allowing the user to use the keys to play..
KeyboardState keyState = Keyboard.GetState();
if (keyState.IsKeyDown(Keys.Up))
{
Sound1.Play();
}
}
public override void Draw(GameTime gameTime)
{
// Draw the piano sprite
Game1.Instance.spriteBatch.Draw(Sprite, Position, null, Color.SkyBlue, Rotation, Center, 1, SpriteEffects.None, 1);
base.Draw(gameTime);
}
}
}