我正在做一个乒乓球游戏。
当球击中任何一个桨时,它会触发声音效果。然而,问题在于 XNA 更新速度为 60 fps。因此,有时我会听到我的 sfx 触发器在初始碰撞期间刚刚开始发出微弱的声音。
我想让 SFX 触发器触发一次,然后不会再次发生,直到:A)球得分或 B)球与对面的球棒相撞。
我可以采取的最佳方法是什么?
这是我的触发器目前在我的球类中的样子:
/// <summary>
/// Draws a rectangle where the ball and bat collide. Also triggers the SFX for when they collide
/// </summary>
private void BatCollisionRectLeft()
{
// For the left bat
if (ballRect.Intersects(leftBat.batRect))
{
rectangle3 = Rectangle.Intersect(ballRect, leftBat.batRect);
hasHitLeftBat = true;
lastHitLeftBat = true;
lasthitRightBat = false;
AudioManager.Instance.PlaySoundEffect("hit2");
Speed += .2f; // Gradually increases the ball's speed each time it connects with the bat
}
}
然后在我的球的更新函数中调用该函数,该函数由 XNA 中的主要游戏类调用。
/// <summary>
/// Updates position of the ball. Used in Update() for GameplayScreen.
/// </summary>
public void UpdatePosition(GameTime gameTime)
{
.......
// As long as the ball is to the right of the back, check for an update
if (ballPosition.X > leftBat.BatPosition.X)
{
// When the ball and bat collide, draw the rectangle where they intersect
BatCollisionRectLeft();
}
// As long as the ball is to the left of the back, check for an update
if (ballPosition.X < rightBat.BatPosition.X)
{ // When the ball and bat collide, draw the rectangle where they intersect
BatCollisionRectRight();
}
........
}