2015-10-18 33 views
1

我很努力地让我的SoundEngine类的函数PlaySound在我想要的地方播放。从SoundEngine类的任意对象运行一个方法

功能需要一个全球性的功能,但作为功能指的是SoundEngine使对象我不能让静态的。

我将展示的一些重要片段:

在游戏世界里我类:

using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework.Content; 
using Microsoft.Xna.Framework.Media; 
using Microsoft.Xna.Framework.Audio; 
using Microsoft.Xna.Framework.Input; 
using System; 
using SoundEngineSpace; 


    public GameWorld(int width, int height, ContentManager Content) 
{ 
    screenWidth = width; 
    screenHeight = height; 
    random = new Random(); 
    gameState = GameState.Playing; 
    block = Content.Load<Texture2D>("block"); 
    font = Content.Load<SpriteFont>("SpelFont"); 
    SoundEffect blockFallSE = Content.Load<SoundEffect>("BlockFallSE"); 
    Song BuildingWallsintheCold = Content.Load<Song>("91 Building Walls in the Cold"); 
    soundEngine = new SoundEngine(); 
    soundEngine.addSound(blockFallSE); 
    soundEngine.addSong(BuildingWallsintheCold); 
    soundEngine.SetSoundVolume(20); 
    soundEngine.SetSongVolume(5); 
    soundEngine.PlaySong(0); 
    grid = new TetrisGrid(block); 

} 

在我TetrisGrid类

using System; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework.Input; 
using SoundEngineSpace; 

...

public void TetNaarBeneden() 
{ 
    soundEngine.PlaySound(0); 
    InPlayGrid.Velocity = new Vector2(0, gridblock.Height); 
    CheckValidLocation(); 
}//Moves tet down 

和声音发动机类:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Audio; 
using Microsoft.Xna.Framework.Media; 

namespace SoundEngineSpace 
{ 
    public class SoundEngine 
    { 
     private void soundEngine() 
     { 

     } 

     BackgroundSound backgroundSound = new BackgroundSound(); 
     SoundEffects soundEffects = new SoundEffects(); 

     public void addSong(Song s) 
     { 
      backgroundSound.AddBackgroundSound(s); 
     } 

     public void addSound(SoundEffect s) 
     { 
      soundEffects.AddSound(s); 
     } 

     public void PlaySong(int s) 
     { 
      backgroundSound.PlayBackgroundSound(s); 
     } 

     public void PlaySound(int s) 
     { 
      soundEffects.PlaySound(s); 
     } 

     public void SetSongVolume(int v) 
     { 
      backgroundSound.SetBackGroundSoundVolume(v); 
     } 

     public void SetSoundVolume(int v) 
     { 
      soundEffects.SetSoundEffectVolume(v); 
     } 

    } 
} 


class BackgroundSound 
{ 
    public static void backGroundSound() 
    { 

    } 
    private List<Song> BackgroundSoundEffects = new List<Song>(); 
    private bool PlayingBackGroundSound = false; 

    public void AddBackgroundSound(Song s)//addsongs to the list 

    { 
     BackgroundSoundEffects.Add(s); 
    } 

    public void PlayBackgroundSound(int s)//plays BackgroundSound based on location in the list 
    { 
     MediaPlayer.IsRepeating = true;//If I use this exact soundengine again, move this to it's own function instead! 
     if (BackgroundSoundEffects.Count() > s) 
     { 
      if (PlayingBackGroundSound) 
       MediaPlayer.Stop(); 
      MediaPlayer.Play(BackgroundSoundEffects.ElementAt(s)); 
      PlayingBackGroundSound = true; 
     } 
     else 
     { 
      Console.WriteLine("Couldent find the BackgroundSound"); 
     } 
    } 
    public void SetBackGroundSoundVolume(int v) 
    { 
     MediaPlayer.Volume = (float)v/100; 
    } 
} 

class SoundEffects 
{ 
    public static void soundeffects() 
    { 

    } 
    private List<SoundEffect> soundEffects = new List<SoundEffect>(); 

    public void AddSound(SoundEffect s)//addsongs to the list 

    { 
     soundEffects.Add(s); 
    } 

    public void PlaySound(int s)//plays sound based on location in the list 
    { 
     if (soundEffects.Count() > s) 
     { 
      SoundEffect ToPlaySound = soundEffects.ElementAt(s); 
      ToPlaySound.Play(); 
     } 
     else 
     { 
      Console.WriteLine("Couldent find the sound"); 
     } 
    } 
    public void SetSoundEffectVolume(int v) 
    { 
     SoundEffect.MasterVolume = (float)v/100; 
    } 
} 

回答

0

我最终作出一个需要存取权限的SounEngine通过SoundEngine作为一个对象放慢参数的构造函数。 然后我可以在对象中创建一个新的soundEngine,我需要它存在并调用:this.soundEngine = soundEngine。

成功复制了soundEngine并让我使用它的功能。

相关问题