ポンゲームを作っています。
ボールがいずれかのパドルに当たると、効果音が鳴ります。ただし、問題はXNAが60fpsを更新することです。そのため、最初の衝突時に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();
}
........
}