首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >soundeffect,XNA 4.0

soundeffect,XNA 4.0
EN

Stack Overflow用户
提问于 2014-01-05 04:16:36
回答 1查看 149关注 0票数 0

这是我第一次编程游戏,我想创造一个声音效果,每次我拍摄一枚导弹,而不是播放声音时,导弹在屏幕上,就在角色射击后。这是密码。请帮帮我,我整晚都试着这样做:

代码语言:javascript
复制
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,在这里,我所有的内容都被加载并绘制到我控制的屏幕上。

代码语言:javascript
复制
{ 
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);
    }
}
}

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-05 13:46:11

代码语言:javascript
复制
protected override void Update(GameTime gameTime)
{

    ....

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

    ....

}

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

Player类中加载声音,就像加载纹理一样。每次使用Play()创建一个新的项目时,都要调用ShootMissiles(gameTime)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20929990

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档