简体   繁体   English

暂停和恢复单人游戏的背景音轨

[英]Pausing and resuming background soundtrack for monogame

I am trying to simply add a pause and resume feature in my ping pong video game project.我试图在我的乒乓视频游戏项目中简单地添加暂停和恢复功能。 I Here is what I have tried:我这是我尝试过的:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using Microsoft.Xna.Framework;
 using Microsoft.Xna.Framework.Audio;
 using Microsoft.Xna.Framework.Content;
 using Microsoft.Xna.Framework.Graphics;
 using Microsoft.Xna.Framework.Input;
 using Microsoft.Xna.Framework.Media;

 namespace Ping_Pong
{
/// <summary>
/// This is the main type for your game
/// </summary>  
public class Game1 : Microsoft.Xna.Framework.Game
 {

 ...
 protected override void Update(GameTime gameTime)
 {


    KeyboardState press = Keyboard.GetState();

    if (press.IsKeyDown(Keys.P)==true)
    {
        pressed = !pressed;

        if (pressed == true)
        {
            MediaPlayer.Pause();
        }
        // do something 
        if (pressed ==false)
        {
            MediaPlayer.Resume();
        }
    }
    // update the ball's location on the screen
    MoveBall();
    // update the paddles' locations on the screen
    MovePaddles();

    base.Update(gameTime);
}
 ...
}
}

when I run this, sometimes it will pause and sometimes it will do nothing.当我运行它时,有时它会暂停,有时它什么也不做。 How can I modify this so that I can make sure that it works properly?如何修改它以确保它正常工作?

IsKeyDown(Keys.P) returns true on every frame that "P" is being held down, not just on a single frame when that key is first pressed. IsKeyDown(Keys.P) 在按住“P”的每一帧上返回 true,而不仅仅是在第一次按下该键时的单个帧上。 Since Update() is probably getting called around 60 times a second, pressed is being flipped back and forth between true and false even if you just tap the P button for a fraction of a second.由于 Update() 每秒可能会被调用 60 次左右,因此即使您只需轻按 P 按钮几分之一秒,pressed 也会在 true 和 false 之间来回切换。 (This is why the code works some of the time; you have about a 50% chance of letting go of the P button on the desired state.) (这就是代码在某些时候有效的原因;您有大约 50% 的机会让所需的 state 上的 P 按钮的 go。)

One way to differentiate between a pressed event that should only fire once and a button being held down continuously is by looking for changes between the current KeyboardState and the previous one.区分应该只触发一次的按下事件和连续按住按钮的一种方法是查找当前 KeyboardState 和前一个 KeyboardState 之间的变化。 This code below will turn pause/unpause the MediaPlayer each time the P key is struck.每次按下 P 键时,下面的代码将暂停/取消暂停 MediaPlayer。

Declare as class members:声明为 class 成员:

private KeyboardState keyboardState, lastKeyboardState;

And then change the relevant code in Update() to:然后将Update()中的相关代码修改为:

keyboardState = Keyboard.GetState();

if (keyboardState.IsKeyDown(Keys.P) && lastKeyboardState.IsKeyUp(Keys.P))
{
    // P button has been pressed - change MediaPlayer state
    if (MediaPlayer.State == MediaState.Paused) // Paused to playing
        MediaPlayer.Resume();
    else if (MediaPlayer.State == MediaState.Playing) // Playing to paused
        MediaPlayer.Pause();
}

lastKeyboardState = keyboardState;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM