我正在制作一个程序,单击按钮即可创建声音,例如那些即时按钮应用程序。但是当我单击按钮时,它不会播放任何声音。我也不知道为什么。我所有的“构建”调试也都成功了。
这些是我的课:
游戏1.cs:
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D Background;
Button button;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
this.Window.Title = "Bengt Slår På Knappar";
graphics.PreferredBackBufferHeight = 720;
graphics.PreferredBackBufferWidth = 1280;
IsMouseVisible = true;
}
/// <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()
{
Background = Content.Load<Texture2D>("bengtbakgrund");
Texture2D Btexture = Content.Load<Texture2D>("Button");
SoundEffect dundundun = Content.Load<SoundEffect>("DUN DUN DUN");
button = new Button(dundundun, Btexture);
spriteBatch = new SpriteBatch(GraphicsDevice);
}
/// <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>
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
Rectangle mouseRect = new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 10, 10);
MouseState mouseState = Mouse.GetState();
if (mouseRect.Intersects(button.rect) && mouseState.LeftButton == ButtonState.Pressed)
{
button.MySound.Play();
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(Background, new Vector2(0,0), Color.White);
button.Draw(spriteBatch, new Vector2(300, 300));
spriteBatch.End();
base.Draw(gameTime);
}
}
按钮.cs:
public class Button
{
Texture2D Texture { get; set; }
public SoundEffect MySound { get; set; }
public Rectangle rect;
public Button(SoundEffect mySound, Texture2D texture)
{
MySound = mySound;
Texture = texture;
}
public void LoadContent(ContentManager Content)
{
}
public void Update(GameTime gameTime)
{
}
public void Draw(SpriteBatch spriteBatch, Vector2 location)
{
Rectangle rect = new Rectangle((int)0, (int)0, Texture.Width, Texture.Height);
spriteBatch.Draw(Texture, location, Color.White);
}
}
我想要做的是有一个基本按钮类,所以如果你想添加一个带有声音的新按钮,你只需写:button = new Button(//sound effect, //texture);
但我无法让它工作。任何帮助,将不胜感激。