2013-10-27 38 views
1

如果我只是告诉歌曲在整个开放菜单& 1st Level中播放(即总是到目前为止),那么播放效果会很好。但是,我希望只有当播放器按下输入并且级别1加载时才播放声音。加载关卡后声音无法播放

相关的代码是在这里:

protected override void LoadContent() 
{ 
    // Create a new SpriteBatch, which can be used to draw textures. 
    spriteBatch = new SpriteBatch(GraphicsDevice); 

    // Opening Screen Textures 
    test = Content.Load<Texture2D>("test"); 

    // Load sounds here 
    if (gameState == GameState.Level1) 
    { 
     backgroundSong = Content.Load<SoundEffect>("Call to Adventure"); 
     SoundEffectInstance backgroundSongInstance = backgroundSong.CreateInstance(); 
     backgroundSongInstance.IsLooped = true; 
     backgroundSong.Play(); 
    } 
} 

所以就像我说的,之前我做了,如果循环,播放的声音就好了。任何想法我做错了什么?游戏从gameState.MenugameState.Level1当用户按下输入(图形加载和除了声音的一切行为)。

全部代码是在这里的情况下,我错过了高于一切:

public class Game1 : Microsoft.Xna.Framework.Game 
{ 
    GraphicsDeviceManager graphics; 
    SpriteBatch spriteBatch; 
    const int WINDOW_WIDTH = 800; 
    const int WINDOW_HEIGHT = 600; 

    // game state 
    GameState gameState = GameState.OpeningMenu; 

    // menu fields 
    Texture2D test; 

    // Level 1 textures list 
    Texture2D skyBackground; 
    //Texture2D dirtTexture; 
    Texture2D grassTexture; 
    Texture2D leftGrassTexture; 
    Texture2D rightGrassTexture; 

    // sounds 
    SoundEffect backgroundSong; 

    // Level 1 platforms 
    Platforms platform1; 
    Platforms platform2; 
    Platforms platform3; 
    Platforms platform4; 
    Platforms platform5; 
    Platforms platform6; 
    Platforms platform7; 
    Platforms platform8; 
    Platforms platform9; 

    // drawing variables 
    int oneWidthUnit = WINDOW_WIDTH/40; 
    int oneHeightUnit = WINDOW_HEIGHT/30; 
    //int twoWidthUnits = WINDOW_WIDTH/20; 
    //int twoHeightUnits = WINDOW_HEIGHT/15; 

    public Game1() 
    { 
     graphics = new GraphicsDeviceManager(this); 
     Content.RootDirectory = "Content"; 

     graphics.PreferredBackBufferWidth = WINDOW_WIDTH; 
     graphics.PreferredBackBufferHeight = WINDOW_HEIGHT; 
     IsMouseVisible = true; 
    } 

    protected override void Initialize() 
    { 
     Window.Title = "Rory's Super Mega Awesome Game of Awesomeness"; 
     base.Initialize(); 
    } 

    protected override void LoadContent() 
    { 
     // Create a new SpriteBatch, which can be used to draw textures. 
     spriteBatch = new SpriteBatch(GraphicsDevice); 

     // Opening Screen Textures 
     test = Content.Load<Texture2D>("test"); 

     // Load sounds here 
     if (gameState == GameState.Play) 
     { 
      backgroundSong = Content.Load<SoundEffect>("Call to Adventure"); 
      SoundEffectInstance backgroundSongInstance = backgroundSong.CreateInstance(); 
      backgroundSongInstance.IsLooped = true; 
      backgroundSong.Play(); 
     } 

     // Load Level 1 sprite textures here 
     skyBackground = Content.Load<Texture2D>("skybackground"); 
     //dirtTexture = Content.Load<Texture2D>("dirt"); 
     grassTexture = Content.Load<Texture2D>("grass_top"); 
     leftGrassTexture = Content.Load<Texture2D>("edge_left"); 
     rightGrassTexture = Content.Load<Texture2D>("edge_right"); 

     //create platforms 
     platform1 = new Platforms(0, 28 * oneHeightUnit, 15, grassTexture, leftGrassTexture, rightGrassTexture); 
     platform2 = new Platforms(26 * oneWidthUnit, 28 * oneHeightUnit, 14, grassTexture, leftGrassTexture, rightGrassTexture); 
     platform3 = new Platforms(10 * oneWidthUnit, 23 * oneHeightUnit, 7, grassTexture, leftGrassTexture, rightGrassTexture); 
     platform4 = new Platforms(18 * oneWidthUnit, 19 * oneHeightUnit, 5, grassTexture, leftGrassTexture, rightGrassTexture); 
     platform5 = new Platforms(5 * oneWidthUnit, 15 * oneHeightUnit, 9, grassTexture, leftGrassTexture, rightGrassTexture); 
     platform6 = new Platforms(19 * oneWidthUnit, 11 * oneHeightUnit, 3, grassTexture, leftGrassTexture, rightGrassTexture); 
     platform7 = new Platforms(23 * oneWidthUnit, 7 * oneHeightUnit, 3, grassTexture, leftGrassTexture, rightGrassTexture); 
     platform8 = new Platforms(30 * oneWidthUnit, 7 * oneHeightUnit, 7, grassTexture, leftGrassTexture, rightGrassTexture); 
     platform9 = new Platforms(34 * oneWidthUnit, 14 * oneHeightUnit, 6, grassTexture, leftGrassTexture, rightGrassTexture); 

    } 

    protected override void Update(GameTime gameTime) 
    { 
     // goes from menu to level 1 when player presses enter 
     if (gameState == GameState.OpeningMenu && Keyboard.GetState().IsKeyDown(Keys.Enter)) 
     { 
      gameState = GameState.Play; 
     } 

     base.Update(gameTime); 
    } 

    protected override void Draw(GameTime gameTime) 
    { 
     GraphicsDevice.Clear(Color.CornflowerBlue); 

     spriteBatch.Begin(); 

     // draw opening menu 
     if (gameState == GameState.OpeningMenu) 
     { 
      Rectangle rec = new Rectangle(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); 
      spriteBatch.Draw(test, rec, Color.White); 
     } 

     // draw level 1 
     else if (gameState == GameState.Play) 
     { 
      DrawScenery(); 

      platform1.Draw(spriteBatch); 
      platform2.Draw(spriteBatch); 
      platform3.Draw(spriteBatch); 
      platform4.Draw(spriteBatch); 
      platform5.Draw(spriteBatch); 
      platform6.Draw(spriteBatch); 
      platform7.Draw(spriteBatch); 
      platform8.Draw(spriteBatch); 
      platform9.Draw(spriteBatch); 
     } 

     spriteBatch.End(); 

     base.Draw(gameTime); 
    } 

    #region Drawing Code 

    // draw the sky 
    private void DrawScenery() 
    { 
     Rectangle backgroundRectangle = new Rectangle(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); 
     spriteBatch.Draw(skyBackground, backgroundRectangle, Color.White); 
    } 

    #endregion 
} 

回答

2

LoadContent通常被调用一次,加载您的内容,同时Update()被称为每秒(取决于)约60倍。你用于检测跳转的代码是否正常,看起来你会希望在更改游戏状态时执行LoadContent中的代码。情况并非如此,代码无法知道你想要这样做(你可以创建一个事件处理程序)。你应该创建一个像PlaySounds()这样的方法,并且(之前)在加载游戏/关卡时调用它。现在,当您按输入

您还应该继续并加载所有需要的内容,因为您不会再次返回到LoadContent()

protected override void LoadContent() 
{ 
    // Create a new SpriteBatch, which can be used to draw textures. 
    spriteBatch = new SpriteBatch(GraphicsDevice); 

    // Opening Screen Textures 
    test = Content.Load<Texture2D>("test"); 

    //LOAD but DONT PLAY sound 
    backgroundSong = Content.Load<SoundEffect>("Call to Adventure"); 
    backgroundSongInstance = backgroundSong.CreateInstance(); 
    backgroundSongInstance.IsLooped = true; 

    //Rest of code cut out for example! 
} 

继续前进,使SoundEffectInstance backgroundSongInstance一个新的变量,只要你拥有了更多的控制权(因为它会被销毁,因为它退出Update()方法的范围,所以后来我们就可以访问它。

在你Update方法:

//Goes from menu to level 1 when player presses enter 
if (gameState == GameState.OpeningMenu && Keyboard.GetState().IsKeyDown(Keys.Enter)) 
{ 
     gameState = GameState.Play; 
     //Start playing sound 
     backgroundSongInstance.Play(); 
} 
+0

TIL LoadContent在比赛中只被调用一次这就是为什么我喜欢这个网站,我同去学习比我预想的要学会更感谢您的帮助,给予好评和正确的。回答授予! – rory

+0

@rory没问题! – Cyral