Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7654811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:21:06+00:00 2026-05-31T12:21:06+00:00

I’m doing a college assignment in XNA to make a virtual piano. I have

  • 0

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);
    }


 }        
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:21:07+00:00Added an answer on May 31, 2026 at 12:21 pm

    In the Game1.LoadContent() method, you need to change

           children.Add(new Piano());  
    

    to

           children.Add(new PianoNote());  
    

    so that when you go to draw the children in Game1.Draw(), this uses the PianoNote’s Draw() implementation, rather than the empty Piano.Draw() base class method.

    The children collection type is fine.

    As a side note – depending how many items end up in this collection, you might want to consider using an alternative to virtual methods, as they are known to be about 40% slower in execution for Xbox360 deployments.

    more @ http://blogs.msdn.com/b/netcfteam/archive/2005/05/04/414820.aspx
    and http://msdn.microsoft.com/en-us/library/bb203912.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.