最初のメニューと 1 番目のレベル (つまり、常にここまで) で曲を再生するように指示すると、問題なく再生されます。ただし、プレーヤーが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 ループを作成する前に、サウンドは問題なく再生されました。私が間違っていることは何か分かりますか?ユーザーが押すgameState.Menu
と、ゲームが開始されます(グラフィックが読み込まれ、サウンド以外のすべてが動作します)。gameState.Level1
Enter
上記の何かを見逃した場合に備えて、完全なコードは次のとおりです。
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
}