C# 游戏的着陆器,不是降落在着陆台上而是经过

C# 游戏的着陆器,不是降落在着陆台上而是经过,c#,xna,C#,Xna,我想让xna游戏的着陆器在着陆台上成功着陆,然后播放声音,但是,它正好通过,声音仍然播放。完整的代码如下: namespace MoonLander { /// <summary> /// This is the main type for your game /// </summary> public class BasicLunarLander : Microsoft.Xna.Framework.Game { Graphi

我想让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);
    }
  }
}
namespace月球登陆者
{
/// 
///这是游戏的主要类型
/// 
公共类BasicLunarLander:Microsoft.Xna.Framework.Game
{         
图形管理器图形;
SpriteBatch SpriteBatch;
SoundEffectInstance EndGameSoundInstance;
纹理2D背景图像;
纹理2D着陆图像;
音效;配音;
国际LanderPosX;
国际地标;
纹理2D着陆板图像;
公共基础设施
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
}
/// 
///允许游戏在开始运行之前执行任何需要的初始化。
///在这里,它可以查询任何必需的服务,并加载任何非图形化的服务
///相关内容。调用base.Initialize将枚举所有组件
///并对它们进行初始化。
/// 
受保护的覆盖无效初始化()
{
//TODO:在此处添加初始化逻辑
LanderPosX=100;
LanderPosY=0;
base.Initialize();
}
/// 
///LoadContent将在每个游戏中调用一次,并且是加载的地方
///你所有的内容。
/// 
受保护的覆盖void LoadContent()
{
//创建一个新的SpriteBatch,可用于绘制纹理。
spriteBatch=新spriteBatch(图形设备);
//TODO:使用此.Content在此处加载游戏内容
BackgroundImage=Content.Load(“背景”);
LandingImage=Content.Load(“着陆器”);
LandingPadImage=Content.Load(“landingpad”);
EndGameSound=Content.Load(“众笑”);
EndGameSoundInstance=EndGameSound.CreateInstance();
}
/// 
///UnloadContent将在每个游戏中调用一次,并且是卸载的地方
///所有内容。
/// 
受保护的覆盖无效UnloadContent()
{
//TODO:在此卸载任何非ContentManager内容
}
/// 
///允许游戏运行逻辑,例如更新世界,
///检查碰撞、收集输入和播放音频。
/// 
///提供计时值的快照。
受保护覆盖无效更新(游戏时间游戏时间)
{
//允许游戏退出
if(GamePad.GetState(PlayerIndex.One).Buttons.Back==ButtonState.Pressed)
这是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:在此处添加更新逻辑
矩形矩形Lander=新矩形(LanderPosX、LanderPosY、LandingImage.Width、LandingImage.Height);
矩形rectLandingPad=新矩形(350500,LandingPadImage.Width,LandingPadImage.Height);
if(矩形着陆器相交(矩形着陆垫))
{
EndGameSoundInstance.Play();
}
更新(游戏时间);
}
/// 
///这就是所谓的比赛应该平局的时候。
/// 
///提供计时值的快照。
受保护覆盖无效绘制(游戏时间游戏时间)
{
//TODO:在此处添加图形代码
spriteBatch.Begin();
spriteBatch.Draw(背景图像,矢量2.0,彩色,白色);
spriteBatch.Draw(LandingImage,新矢量2(LanderPosX,LanderPosY),彩色,白色);
spriteBatch.Draw(LandingPadImage,新矢量2(350500),彩色,白色);
spriteBatch.End();
基础。抽签(游戏时间);
}
}
}

希望您能在这个问题上帮助我,谢谢大家抽出时间。

这是因为您在着陆器着陆后没有采取任何措施防止移动(条件已满足)。声音当然会播放,因为这就是你编写的代码

添加一个变量,检测它是否已登陆,并在类中声明它

bool _hasLanded = false;
然后在更新方法(用于上、下、左和右键)中包装键检测,如果:

if(!_hasLanded)
{
//keypresses go here
}
最后,在检查矩形是否相交的
if
中,添加

_hasLanded = true;
这将阻止你在着陆器接触着陆垫后移动它