如果我只是在整个开场菜单中播放该歌曲。第一级(即,总是到目前为止)然后它播放得很好。但是,我希望只有当玩家按下回车并且等级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
}
答案 0 :(得分:2)
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();
}