2012-03-16 79 views
0

我想讓這個XNA遊戲的着陸器成功着陸在着陸墊上,然後播放聲音,但是,它通過並通過聲音仍然播放。完整的代碼是:着陸器的遊戲,不着陸在着陸墊上,但通過

namespace MoonLander 
{ 
    /// <summary> 
    /// This is the main type for your game 
    /// </summary> 
    public class BasicLunarLander : Microsoft.Xna.Framework.Game 
    {   
    GraphicsDeviceManager graphics; 
    SpriteBatch spriteBatch; 
    SoundEffectInstance EndGameSoundInstance; 
    Texture2D BackgroundImage; 
    Texture2D LandingImage; 
    SoundEffect EndGameSound; 
    int LanderPosX; 
    int LanderPosY; 
    Texture2D LandingPadImage; 



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

    /// <summary> 
    /// Allows the game to perform any initialization it needs to before starting to run. 
    /// This is where it can query for any required services and load any non-graphic 
    /// related content. Calling base.Initialize will enumerate through any components 
    /// and initialize them as well. 
    /// </summary> 
    protected override void Initialize() 
    { 
     // TODO: Add your initialization logic here 
     LanderPosX = 100; 
     LanderPosY = 0; 

     base.Initialize(); 
    } 

    /// <summary> 
    /// LoadContent will be called once per game and is the place to load 
    /// all of your content. 
    /// </summary> 
    protected override void LoadContent() 
    { 
     // Create a new SpriteBatch, which can be used to draw textures. 
     spriteBatch = new SpriteBatch(GraphicsDevice); 



     // TODO: use this.Content to load your game content here 
     BackgroundImage = Content.Load<Texture2D>("background"); 
     LandingImage = Content.Load<Texture2D>("lander"); 
     LandingPadImage = Content.Load<Texture2D>("landingpad"); 
     EndGameSound = Content.Load<SoundEffect>("crowdcheer"); 
     EndGameSoundInstance = EndGameSound.CreateInstance(); 

    } 

    /// <summary> 
    /// UnloadContent will be called once per game and is the place to unload 
    /// all content. 
    /// </summary> 
    protected override void UnloadContent() 
    { 
     // TODO: Unload any non ContentManager content here 
    } 

    /// <summary> 
    /// Allows the game to run logic such as updating the world, 
    /// checking for collisions, gathering input, and playing audio. 
    /// </summary> 
    /// <param name="gameTime">Provides a snapshot of timing values.</param> 
    protected override void Update(GameTime gameTime) 
    { 
     // Allows the game to exit 
     if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 
      this.Exit(); 


     if (Keyboard.GetState().IsKeyDown(Keys.Down)) 
     { 
      LanderPosY = LanderPosY + 2; 
     } 

     if (Keyboard.GetState().IsKeyDown(Keys.Up)) 
     { 
      LanderPosY = LanderPosY - 2; 
     } 

     if (Keyboard.GetState().IsKeyDown(Keys.Left)) 
     { 
      LanderPosX = LanderPosX - 2; 
     } 

     if (Keyboard.GetState().IsKeyDown(Keys.Right)) 
     { 
      LanderPosX = LanderPosX + 2; 
     } 

     // TODO: Add your update logic here 
     Rectangle rectLander = new Rectangle(LanderPosX, LanderPosY, LandingImage.Width, LandingImage.Height); 
     Rectangle rectLandingPad = new Rectangle(350, 500, LandingPadImage.Width, LandingPadImage.Height); 

     if (rectLander.Intersects(rectLandingPad)) 
     { 
      EndGameSoundInstance.Play(); 

     } 

     base.Update(gameTime); 
    } 

    /// <summary> 
    /// This is called when the game should draw itself. 
    /// </summary> 
    /// <param name="gameTime">Provides a snapshot of timing values.</param> 
    protected override void Draw(GameTime gameTime) 
    { 



     // TODO: Add your drawing code here 
      spriteBatch.Begin(); 
     spriteBatch.Draw(BackgroundImage, Vector2.Zero, Color.White); 
     spriteBatch.Draw(LandingImage, new Vector2(LanderPosX, LanderPosY), Color.White); 
     spriteBatch.Draw(LandingPadImage, new Vector2(350,500), Color.White); 
     spriteBatch.End(); 
     base.Draw(gameTime); 
    } 
    } 
} 

希望你能幫助我解決這個問題,謝謝大家的時間。

回答

0

這是因爲在着陸器着陸後(條件已滿足),你沒有做任何有關防止移動的事情。聲音播放當然,因爲那是你做的代碼。

添加量檢測,如果它已經登陸,並宣佈它的類中

bool _hasLanded = false; 

然後包裹在你的更新方法的關鍵檢測(爲上,下,左,右方向鍵)內,如果:

if(!_hasLanded) 
{ 
//keypresses go here 
} 

最後,你裏面哪裏if您檢查,看看是否矩形相交,加

_hasLanded = true; 

這應該可以防止您在着陸器接觸到着陸墊後能夠移動着陸器。