C#(XNA框架)虚拟钢琴代码编译,但未出现。输出提供了我指定的背景,没有其他内容

C#(XNA框架)虚拟钢琴代码编译,但未出现。输出提供了我指定的背景,没有其他内容,c#,xna,polymorphism,C#,Xna,Polymorphism,我正在XNA做一个虚拟钢琴的大学作业。 我有两门课,钢琴(我的基础课)和钢琴(我的子课) 我在钢琴中声明了一些值,这些值在钢琴中使用(继承) 和重写使用多态性的函数 然而,当我在Game1文件中调用draw函数时(我相信相当于main), 什么也没有出现。 有人能告诉我哪里出了问题吗 这是我的代码-游戏1文件 using System; using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framewo

我正在XNA做一个虚拟钢琴的大学作业。 我有两门课,钢琴(我的基础课)和钢琴(我的子课)

我在钢琴中声明了一些值,这些值在钢琴中使用(继承) 和重写使用多态性的函数

然而,当我在Game1文件中调用draw函数时(我相信相当于main), 什么也没有出现。 有人能告诉我哪里出了问题吗

这是我的代码-游戏1文件

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);
      }
   }
 }
钢琴音符文件

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


 }        
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用Microsoft.Xna.Framework;
使用Microsoft.Xna.Framework.Audio;
使用Microsoft.Xna.Framework.Content;
使用Microsoft.Xna.Framework.GamerServices;
使用Microsoft.Xna.Framework.Graphics;
使用Microsoft.Xna.Framework.Input;
使用Microsoft.Xna.Framework.Media;
名称空间1
{
钢琴类:钢琴//扩展了钢琴,它是钢琴的一个子类
{
公共覆盖无效加载内容()
{
//从内容管道加载音频文件。无需包括文件类型
//--------------钢琴位图----------------------------------
位置X=100;
位置Y=5;
Sprite=Game1.Instance.Content.Load(“piano_示例”);
中心X=精灵宽度/2;
中心Y=精灵高度/2;
旋转=0;
//------------------------------------------------------------
Sound1=Game1.Instance.Content.Load(“Oct1NoteC”);
//Sound2=Game1.Instance.Content.Load(“Oct1NoteC#”);
//Sound=Game1.Instance.Content.Load(“Oct1NoteD”);
//Sound=Game1.Instance.Content.Load(“Oct1NoteD#”);
//Sound=Game1.Instance.Content.Load(“Oct1NoteE”);
//Sound=Game1.Instance.Content.Load(“Oct1NoteF”);
//Sound=Game1.Instance.Content.Load(“Oct1NoteF#”);
//Sound=Game1.Instance.Content.Load(“Oct1NoteG”);
//Sound=Game1.Instance.Content.Load(“Oct1NoteG#”);
//Sound=Game1.Instance.Content.Load(“Oct1NoteA”);
//Sound=Game1.Instance.Content.Load(“Oct1NoteA#”);
//Sound=Game1.Instance.Content.Load(“Oct1NoteB”);
//------------倍频程2-----------------------
//Sound=Game1.Instance.Content.Load(“Oct2NoteC”);
//Sound=Game1.Instance.Content.Load(“Oct2NoteC#”);
//Sound=Game1.Instance.Content.Load(“Oct2NoteD”);
//Sound=Game1.Instance.Content.Load(“Oct2NoteD#”);
//Sound=Game1.Instance.Content.Load(“Oct2NoteE”);
//Sound=Game1.Instance.Content.Load(“Oct2NoteF”);
//Sound=Game1.Instance.Content.Load(“Oct2NoteF#”);
//Sound=Game1.Instance.Content.Load(“Oct2NoteG”);
//Sound=Game1.Instance.Content.Load(“Oct2NoteG#”);
//Sound=Game1.Instance.Content.Load(“Oct2NoteA”);
//Sound=Game1.Instance.Content.Load(“Oct2NoteA#”);
//Sound=Game1.Instance.Content.Load(“Oct2NoteB”);
base.LoadContent();
}
公共覆盖无效更新(游戏时间游戏时间)
{
更新(游戏时间);
//获取键盘的状态,允许用户使用这些键进行播放。。
KeyboardState keyState=Keyboard.GetState();
if(keyState.IsKeyDown(Keys.Up))
{
声音1.播放();
}
}
公共覆盖无效绘制(游戏时间游戏时间)
{
//画钢琴精灵
Game1.Instance.spriteBatch.Draw(精灵、位置、空值、颜色、天蓝色、旋转、中心、1、精灵效果、无、1);
基础。抽签(游戏时间);
}
}        
}
在Game1.LoadContent()方法中,您需要更改

       children.Add(new Piano());  

因此,当您在Game1.draw()中绘制子对象时,将使用PianoNote的draw()实现,而不是空的Piano.draw()基类方法

“儿童”集合类型很好

作为一个旁注-根据这个集合中有多少个项目结束,您可能需要考虑使用一个替代虚拟方法,因为它们在XBOX360部署中的执行速度大约慢了40%。 更多@


或者他可以填充钢琴绘图功能,让它绘制所有音符。钢琴是钢琴的控制者。也许他以后会在Piano的更新方法中添加一些东西?封装!!!!!请检查他的代码;Game1类目前拥有一套钢琴收藏,PianoNote源自钢琴类——钢琴类在不知道所有音符的情况下如何“绘制所有音符”?目前只有Game1类可以。他只是简单地将基类实例放在children.Add()方法中,而不是放在具体的类中。@Shingesu:我应该注意,这可能并不理想,但看起来他只是在进行原型设计并使某些东西工作,我要做的是让他的代码工作。还指出可能有更好的方法(你可能有更好的想法)嗯,我并没有全部读过。但是技术上(如果他遵循OO标准)应该是这样的。。。我认为他在“是a/有a”中摇摆不定,但我懒得写一个完整的答案来解释这一切。与这个问题也没有真正的联系。您可能会添加一个旁注,说明这通常是可以看到Piano类中SoundEffect类级别变量堆看起来似乎正在使用的方式,但在PianoNote实现设置它们之前,它们不会加载内容(也只有一个SoundEffect)。也许音效可以用钢琴来封装(因为我假设音符的数量有限),或者更一般的东西。但你是对的,我们正在进入oo原则,这与我回答的要点无关;只是让他的代码正常工作。请以后不要记下您尚未完全理解的源代码的答案。
       children.Add(new Piano());  
       children.Add(new PianoNote());