2014-01-05 6 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(); 
    } 

    .... 

} 

게임의 모든 단일 프레임/틱은 현재에 대한 참조를 저장하는 모든 총알 사운드를 재생하려는 (나는이 1 초에 60 번입니다 가정). 기본적으로 60 번 이상 소리를 내고 있습니다.

텍스처를로드하는 것과 똑같은 방법으로 사운드를 Player 클래스에로드하십시오. ShootMissiles(gameTime)으로 새 글 머리 기호를 만들 때마다 Play()으로 전화하십시오.

+0

정말 고마워요! 알았어, 효과가있어. 내가 올바른 방법으로 해본 결과, 그것이 아주 기본적인 실수라는 것을 알았습니다. –

+0

안녕하세요. @Joe 저는이 일을^_ ^하고 있습니다. 처음으로 유용 할 수도 있습니다. p http://www.youtube.com/watch?v=Rcg6dxNKicw&feature=youtu.be –