C# 音效,XNA 4.0

C# 音效,XNA 4.0,c#,xna,soundeffect,C#,Xna,Soundeffect,这是我第一次编写一个游戏,我想在我每次射击一枚导弹时创造一个声音效果,而不是在导弹出现在屏幕上时播放声音,就在角色射击之后。这是代码。请帮帮我,我整晚都在努力: class Player { Texture2D hero; Vector2 hero_location; KeyboardState hero_movement; int missileREA; public List<adw> missiles = new List<adw&

这是我第一次编写一个游戏,我想在我每次射击一枚导弹时创造一个声音效果,而不是在导弹出现在屏幕上时播放声音,就在角色射击之后。这是代码。请帮帮我,我整晚都在努力:

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; }
}
职业玩家
{
英雄;
矢量2的位置;
键盘状态英雄运动;
国际导弹协会;
公共列表=新列表();
内容管理器获取内容;
公共void加载内容(ContentManager内容)
{
hero=Content.Load(“F5S4”);
英雄位置=矢量2.0;
内容=内容;
}
公共空射导弹(游戏时间游戏时间)
{
adw导弹=新的adw();
导弹装载量(takeContent);
导弹。导弹位置=英雄位置+新矢量2(英雄宽度/2,-50);
添加(导弹);
射击=导弹;
}
公开作废更新(游戏时间游戏时间)
{
hero_movement=Keyboard.GetState();
如果(英雄动作.IsKeyDown(键.W))
{
位置Y-=5;
}
如果(英雄动作。IsKeyDown(键D))
{
英雄_位置X+=5;
}
if(英雄动作IsKeyDown(按键S))
{
英雄_位置Y+=3;
}
如果(英雄动作.IsKeyDown(键A))
{
英雄_位置X-=5;
}
如果(英雄动作.IsKeyDown(键.NumPad1))
{
Misselerea+=gameTime.ElapsedGameTime.millides;
如果(导弹面积>200)
{
Misselerea=0;
射击导弹(游戏时间);
}
}
foreach(导弹中的adw导弹)
{
导弹。更新(游戏时间);
}
}
公共作废抽签(SpriteBatch SpriteBatch)
{
绘制(英雄、英雄位置、颜色、白色);
foreach(导弹中的adw导弹)
{
导弹.牵引(spriteBatch);
}
}
公共IEnumerable属性{get;set;}
公共列表快照{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);
    }
}
}
{
公共类游戏1:Microsoft.Xna.Framework.Game
{
图形管理器图形;
SpriteBatch SpriteBatch;
玩家;
声效导弹1;
公共游戏1()
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
}
受保护的覆盖无效初始化()
{
player=新玩家();
graphics.PreferredBackBufferWidth=1024;
graphics.PreferredBackBufferHeight=680;
graphics.ApplyChanges();
base.Initialize();
}
受保护的覆盖void LoadContent()
{
spriteBatch=新spriteBatch(图形设备);
missile1=Content.Load(“missile1”);
player.LoadContent(Content);
}
受保护的覆盖无效UnloadContent()
{
}
受保护覆盖无效更新(游戏时间游戏时间)
{
if(GamePad.GetState(PlayerIndex.One).Buttons.Back==ButtonState.Pressed)
这是Exit();
foreach(玩家中的adw导弹。导弹)
{
任务1.玩();
}
玩家更新(游戏时间);
更新(游戏时间);
}
受保护覆盖无效绘制(游戏时间游戏时间)
{
图形设备。清晰(颜色:矢车菊蓝);
spriteBatch.Begin();
球员。抽签(斯皮特巴特);
spriteBatch.End();
基础。抽签(游戏时间);
}
}
}
如果这不是问这样问题的地方,就告诉我。我会照你的建议去做

protected override void Update(GameTime gameTime)
{

    ....

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

    ....

}
游戏中的每一帧/滴答声(我假设这是每秒60次)您都试图为当前存储引用的每一颗子弹播放声音。基本上,你每秒播放一个声音60多次


播放器
类中加载声音,与加载纹理的方式完全相同。每次使用射击导弹(游戏时间)创建一个新子弹时,调用
Play()

你试过什么?这段代码中没有任何声音用法的参考。@Joe谢谢你的回答。我编辑了我的问题。我希望你能帮忙。再次感谢,非常感谢!!!我明白了,成功了。在我做对了之后,我意识到这是一个非常基本的错误。也许第一次很好:p