gpt4 book ai didi

c# - 加载关卡后声音无法播放

转载 作者:行者123 更新时间:2023-12-02 22:57:45 24 4
gpt4 key购买 nike

如果我只是告诉歌曲在整个菜单和第一级中播放(即一直如此),那么它的播放就很好。但是,我希望声音仅在播放器按Enter并加载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();
}
}

就像我说的那样,在进行if循环之前,声音播放得很好。知道我在做什么错吗?当用户按下Enter键时,游戏从 gameState.Menu变为 gameState.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
}

最佳答案

LoadContent通常被调用一次以加载您的内容,而Update()每秒被调用约60次(视情况而定)。您用于检测跳跃的代码很好,并且看起来您希望更改游戏状态时会执行LoadContent中的代码。事实并非如此,代码无法知道您要执行此操作(可以创建事件处理程序)。您可能应该创建一个类似于PlaySounds()的方法,并且(以前)在加载游戏/关卡时会调用它。现在,当您按Enter键时,它将调用它

您也应该继续加载所有需要的内容,因为您不会再返回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();
}

关于c# - 加载关卡后声音无法播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19617994/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com