在這裏工作,我迄今爲我的遊戲做了一個菜單系統。我添加了三個不同的按鈕。除了一件事外,我已經整理了一切。XNA 2D音效鼠標盤旋
所以,我正在使用正常的if intersections方法來查看鼠標和按鈕矩形是否發生碰撞來播放聲音效果。但是,我不知道如何阻止這種聲音,它只是一直循環播放,直到我將鼠標從按鈕上移開。我想讓它只玩一次。
public void Update(MouseState mouse, GraphicsDevice graphics, SoundEffect soundEffect)
{
rectangle = new Rectangle((int)position.X, (int)position.Y, (int)size.X, (int)size.Y);
Rectangle mouseRectangle = new Rectangle(mouse.X, mouse.Y, 1, 1);
if(mouseRectangle.Intersects(rectangle))
{
if (mouse.LeftButton == ButtonState.Pressed) isClicked = true;
size = new Vector2(graphics.Viewport.Width/9, graphics.Viewport.Height/13);
soundEffect.Play();
}
else
{
size = new Vector2(graphics.Viewport.Width/10, graphics.Viewport.Height/14);
isClicked = false;
}
任何幫助將被apreciated。
順便說一句:這不是必須的,但當我將鼠標懸停在按鈕上時,我又遇到了另一個「問題」,它們變得更大了。但它並沒有從中心變大。這很難解釋,但在x和y位置變大,而不是-x和-y。它有相同的位置。
我覺得聲音有'.looping'布爾屬性。嘗試將其設置爲false。 – user1306322
至於按鈕,當光標首先與它們相交時,也將它們的位置改變爲左上角大小差異的一半。像'button.position - = new Vector2(diff/2,diff/2)',然後在離開按鈕時做相反的事情。 – user1306322