我正在创建一个程序,其中一个按钮可以创建声音,就像那些即时按钮应用程序一样。但是当我点击按钮时,它不会播放任何声音。我也不知道为什么。 我的所有“Build”调试也都取得了成功。
这些是我的课程:
Game1.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);
}
}
Button.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);
但我无法让它发挥作用。任何帮助将不胜感激。
答案 0 :(得分:0)
您处理按钮位置的方式不正确,我不确定它是如何编译的。在draw方法中,您可以定义已存在的局部变量rect
。即使它确实如此,你也没有考虑按钮的实际位置。
我认为你也可以将按钮更新逻辑移到课堂上,所以我会提出我认为你需要做的事情。
公共类Button { //属性 private Texture2D Texture {get;组; } 私人SoundEffect Sound {get;组; } private Rectangle Rectangle {get;组; }
public Button(SoundEffect sound, Texture2D texture, Rectangle position)
{
Sound = sound;
Texture = texture;
Rectangle = position;
}
public void Update(GameTime gameTime, MouseState mouseState)
{
//If mouse is down and the rectangle contains the mouse point (Better for points than Intersect())
if (mouseState.LeftButton == ButtonState.Pressed && Rectangle.Contains(new Point(mouseState.X,mouseState.Y))
{
Sound.Play()
}
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Texture, rectangle, Color.White);
}
}
然后您可以button.Update(Mouse.GetState())
(或者如果您已经使用了另一个MouseState)
看起来绘图位置与按钮的实际位置(绘制方法)有些混淆
你需要确保按钮知道它在屏幕上的位置,以及它的宽度和高度。
button = new Button(dundundun, Btexture, new Rectangle(300, 300, Btexture.Width, Btexture.Height);
这将使按钮位于300,500
并成为纹理的默认大小,现在您的声音按钮已完成:)
我还建议您学习并使用event handlers,这样您就可以轻松添加按钮的事件,例如button.Click += //Do something