这是我第一次编写游戏时,我想在每次射击导弹时产生声音效果,而不是在导弹在屏幕上时播放声音,就在角色射击之后。这是代码。请帮助我,我整晚都试着这样做:
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 :(得分:1)
protected override void Update(GameTime gameTime)
{
....
foreach (adw missile in player.missiles)
{
missile1.Play();
}
....
}
你的游戏的每一帧/刻度(我假设这是每秒60次)你试图为你当前存储引用的每个子弹播放你的声音。基本上,你每秒播放的声音超过60次。
将声音加载到Player
类中,与加载纹理的方式完全相同。每次使用Play()
;
ShootMissiles(gameTime)