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

本文关键字:背景 其他 输出 钢琴 虚拟 代码 编译 XNA 框架 | 更新日期: 2023-09-27 18:27:42

我正在XNA做一项大学作业,制作一架虚拟钢琴。我有两个班,钢琴(我的基础班)和钢琴笔记(我的子班)

我有在Piano中声明的值,这些值在PianoNotes(继承)中使用以及重写使用多态性的函数。

然而,当我调用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
   {
           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)
    {
    }

}
}

钢琴音符文件

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

 }        
}

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

在Game1.LoadContent()方法中,您需要更改

       children.Add(new Piano());  

       children.Add(new PianoNote());  

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

儿童收藏类型很好。

顺便说一句,根据这个集合中最终有多少项,您可能需要考虑使用虚拟方法的替代方法,因为众所周知,Xbox360部署的执行速度要慢40%左右。

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