2014-01-05 108 views
0

這是我第一次編程,我每次拍攝導彈時都會產生聲音效果,當導彈在屏幕上時不會彈出聲音,就在角色開槍後它。這是代碼。請幫我,我試着做了整整一夜:soundeffect,XNA 4.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); 
    } 
} 
} 

如果這不是問這樣的事情的地方,告訴我。我會照你的意見去做。

+0

你有什麼試過?此代碼中沒有提及任何類型的聲音用法。 –

+0

@Joe謝謝你回答我。我編輯了我的問題。我希望你能幫上忙。再一次,謝謝你, –

回答

1
protected override void Update(GameTime gameTime) 
{ 

    .... 

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

    .... 

} 

你的遊戲的每一個幀/蜱(我想這是60次),你正在努力發揮每一個子彈你目前存儲參考您的聲音。基本上,你每秒播放60次以上的聲音。

在您的Player類中加載聲音,完全與加載紋理的方式完全相同。每次使用ShootMissiles(gameTime)創建新項目符號時,請撥打Play();

+0

非常感謝你!我知道了,它工作。在我做到了正確的方式後,我意識到這是一個非常基本的錯誤。 –

+0

嘿@Joe這就是我正在做的^ _ ^。也許第一次很好:p http://www.youtube.com/watch?v=Rcg6dxNKicw&feature=youtu.be –