从SoundEngine类的对象运行一个方法

时间:2015-10-18 13:26:24

标签: c#

我正在努力让我的SoundEngine课程的功能PlaySound在我想要的地方播放。

该函数需要是一个全局函数,但我无法使其成为静态函数,因为函数引用了SoundEngine生成的对象。

我将展示重要的片段:

在我的GameWorld课程中:

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

和soundEngine课程:

    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;
    }
}

1 个答案:

答案 0 :(得分:0)

我最终使得需要访问SounEngine的对象的构造函数将SoundEngine作为参数传递。 然后我可以在我需要它的对象中创建一个新的soundEngine来存在并调用:this.soundEngine = soundEngine。

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