C# 连续停止更新一次以上
我在打乒乓球 当球击中任何一个桨叶时,就会触发音效。然而,问题是XNA以每秒60帧的速度更新。所以,有时我会听到我的sfx触发器在最初的碰撞中开始发出微弱的声音 我想让SFX触发器触发一次,然后直到: A) 球得分了 或 B) 球与对方球棒相撞 我能采取的最好的方法是什么 以下是我的触发器当前在我的ball类中的样子:C# 连续停止更新一次以上,c#,xna,C#,Xna,我在打乒乓球 当球击中任何一个桨叶时,就会触发音效。然而,问题是XNA以每秒60帧的速度更新。所以,有时我会听到我的sfx触发器在最初的碰撞中开始发出微弱的声音 我想让SFX触发器触发一次,然后直到: A) 球得分了 或 B) 球与对方球棒相撞 我能采取的最好的方法是什么 以下是我的触发器当前在我的ball类中的样子: /// <summary> /// Draws a rectangle where the ball and bat collide. Als
/// <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
}
}
//
///在球和球拍碰撞的地方绘制一个矩形。在碰撞时也会触发SFX
///
私有void BatCollisionRectLeft()
{
//左击
if(ballRect.相交(leftBat.batRect))
{
矩形3=矩形.Intersect(ballRect,leftBat.batRect);
hasHitLeftBat=true;
lastHitLeftBat=true;
lasthitRightBat=false;
AudioManager.Instance.PlaySoundEffect(“hit2”);
速度+=.2f;//每次球与球棒连接时,逐渐增加球的速度
}
}
然后在my ball的更新函数中调用该函数,该函数由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();
}
........
}
//
///更新球的位置。在GameplayScreen的Update()中使用。
///
公共无效更新位置(游戏时间游戏时间)
{
.......
//只要球在后面的右边,检查更新
if(ballPosition.X>leftBat.BatPosition.X)
{
//当球和球拍碰撞时,在它们相交的地方绘制矩形
BatCollisionRectLeft();
}
//只要球在后面的左侧,检查更新
if(球位置.X<右击球位置.X)
{//当球和球棒碰撞时,在它们相交的地方画一个矩形
BatCollisionRectRight();
}
........
}
解决方案看起来像这样。这段代码没有经过测试,也没有功能,它只是为了这个想法而存在的。低音方面,你有一个计时器,它会等待一段时间(毫秒),然后再播放声音;要再次播放声音,必须满足碰撞检测要求。这是一个简单的延迟计时器
float timer = 0, timerInterval = 100;
bool canDoSoundEffect = true;
...
void Update(GameTime gt) {
float delta = (float)gt.ElapsedTime.TotalMilliseconds;
// Check if we can play soundeffect again
if (canDoSoundEffect) {
// Play soundeffect if collided and set flag to false
if (ballRect.Intersects(leftBat.batRect)) {
... Collision logic here...
AudioManager.Instance.PlaySoundEffect("hit2");
canDoSoundEffect = false;
}
}
// So we can't play soundeffect - it means that not enough time has passed
// from last play so we need to increase timer and when enough time passes,
// we set flag to true and reset timer
else {
timer += delta;
if (timer >= timerInterval) {
timer -= timerInterval;
canDoSoundEffect = true;
}
}
}
您可以使用一个布尔变量来指示以前的碰撞状态 如果myPreviousCollisionState=
false
,而myCurrentCollisionState=true
->
播放声音、改变方向等
在更新调用结束时,您可以设置
myPreviousCollisionState=myCurrentCollisionState
显然,在游戏开始时,myPreviousCollisionState=false
使用该代码,您的声音将仅在碰撞的第一帧上播放
希望这有帮助 谢谢大家!!简洁明了。今晚我会试一试,让你知道它是怎么回事,一开始我就批准作为答案!我喜欢你在这里做这件事的方式。今晚我也会试试,让你知道它是如何工作的。再次感谢你!