我在做一個pong遊戲。停止發生不止一次的更新,連續發生
當球撞擊任何一個槳時,它會觸發聲音效果。然而,問題在於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();
}
........
}
謝謝!清晰簡潔。今晚我會試一試,讓你知道它是如何發生的,並且一旦我開始行動就批准作爲答案! –