0

这是我第一次编写游戏,我想在每次发射导弹时创建声音效果,而不是在导弹出现在屏幕上时播放声音,就在角色射击之后。这是代码。请帮助我,我试图整晚都这样做:

class Player
{
    Texture2D hero;
    Vector2 hero_location;
    KeyboardState hero_movement;
    int missileREA;
    public List<adw> missiles = new List<adw>();
    ContentManager takeContent;

    public void LoadContent(ContentManager Content)
    {
        hero = Content.Load<Texture2D>("F5S4");
        hero_location = Vector2.Zero;
        takeContent = Content;
    }
    public void ShootMissiles(GameTime gameTime)
    {
        adw missile = new adw();
        missile.LoadContent(takeContent);
        missile.missile_location = hero_location + new Vector2(hero.Width /2,-50);
        missiles.Add(missile);
        shot = missiles;
    }

    public void Update(GameTime gameTime)
    {
        hero_movement = Keyboard.GetState();

        if (hero_movement.IsKeyDown(Keys.W))
        {
            hero_location.Y -= 5;
        }
        if (hero_movement.IsKeyDown(Keys.D))
        {
            hero_location.X += 5;
        }
        if (hero_movement.IsKeyDown(Keys.S))
        {
            hero_location.Y += 3;
        }
        if (hero_movement.IsKeyDown(Keys.A))
        {
            hero_location.X -= 5;
        }
        if (hero_movement.IsKeyDown(Keys.NumPad1))
        {
            missileREA += gameTime.ElapsedGameTime.Milliseconds;
            if (missileREA > 200)
            {
                missileREA = 0;
                ShootMissiles(gameTime);
            }
        }
        foreach (adw missile in missiles)
        {
            missile.Update(gameTime);
        }
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(hero, hero_location, Color.White);
        foreach (adw missile in missiles)
        {
            missile.Draw(spriteBatch);
        }
    }

    public IEnumerable<adw> ShootMissile { get; set; }

    public List<adw> shot { get; set; }
}

这是我的 Game1.cs,我的所有内容都在我的控制下加载并绘制到屏幕上。

{ 
public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Player player;
    SoundEffect missile1;
    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    protected override void Initialize()
    {

        player = new Player();
        graphics.PreferredBackBufferWidth = 1024;
        graphics.PreferredBackBufferHeight = 680;
        graphics.ApplyChanges();

        base.Initialize();
    }

    protected override void LoadContent()
    {

        spriteBatch = new SpriteBatch(GraphicsDevice);

        missile1 = Content.Load<SoundEffect>("missile1");



        player.LoadContent(Content);
    }


    protected override void UnloadContent()
    {

    }

    protected override void Update(GameTime gameTime)
    {

        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();



        foreach (adw missile in player.missiles)
        {
            missile1.Play();
        }

        player.Update(gameTime);

        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);


        spriteBatch.Begin();
        player.Draw(spriteBatch);
        spriteBatch.End();

        base.Draw(gameTime);
    }
}
}

如果这里不是问这种问题的地方,请告诉我。我会按照你的建议去做。

4

1 回答 1

1
protected override void Update(GameTime gameTime)
{

    ....

    foreach (adw missile in player.missiles)
    {
        missile1.Play();
    }

    ....

}

您游戏的每一帧/滴答声(我假设这是每秒 60 次)您都在尝试为您当前存储参考的每个子弹播放声音。基本上,您每秒播放声音 60 次以上。

在类中加载声音Player,与加载纹理的方式完全相同。Play()每次使用ShootMissiles(gameTime);创建新项目符号时调用它

于 2014-01-05T13:46:11.317 回答