C# 从SoundEngine类运行方法';任何地方都有物体
我正在努力让我的SoundEngine类的函数PlaySound在我想要的地方播放 该函数需要是一个全局函数,但我不能使其成为静态函数,因为该函数引用SoundEngine生成的对象 我将展示重要的代码片段: 在我的游戏世界课程中:C# 从SoundEngine类运行方法';任何地方都有物体,c#,C#,我正在努力让我的SoundEngine类的函数PlaySound在我想要的地方播放 该函数需要是一个全局函数,但我不能使其成为静态函数,因为该函数引用SoundEngine生成的对象 我将展示重要的代码片段: 在我的游戏世界课程中: using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Fra
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);
}
和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;
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用Microsoft.Xna.Framework;
使用Microsoft.Xna.Framework.Audio;
使用Microsoft.Xna.Framework.Media;
命名空间SoundEngineSpace
{
公共级声音引擎
{
私有void soundEngine()
{
}
背景声音背景声音=新背景声音();
SoundEffects SoundEffects=新的SoundEffects();
公共歌曲(歌曲s)
{
背景声音。添加背景声音;
}
公共空间添加声音(声音效果s)
{
声音效果。添加声音;
}
公共播放歌曲(INTS)
{
背景声音。播放背景声音;
}
公共空间播放声音(INTS)
{
音效。播放声音;
}
公共交通量(int v)
{
背景声音。背景声音音量(v);
}
公共音量(整数v)
{
声音效果。设置声音效果音量(v);
}
}
}
课堂背景音
{
公共静态无效背景声音()
{
}
私有列表BackgroundSoundEffects=新列表();
private bool PlayingBackGroundSound=假;
public void AddBackgroundSound(歌曲)//将歌曲添加到列表中
{
背景声音效果。添加;
}
public void PlayBackgroundSound(int s)//根据列表中的位置播放背景声音
{
MediaPlayer.IsRepeating=true;//如果我再次使用这个精确的声音引擎,请将其移到它自己的函数!
if(BackgroundSoundEffects.Count()>s)
{
如果(播放背景声音)
MediaPlayer.Stop();
MediaPlayer.Play(BackgroundSoundEffects.ElementAt);
播放背景声音=真;
}
其他的
{
Console.WriteLine(“找不到背景音”);
}
}
公共空间背景音量(int v)
{
MediaPlayer.Volume=(浮动)v/100;
}
}
类音效
{
公共静态音效()
{
}
私有列表soundEffects=新列表();
public void AddSound(SoundEffects)//将歌曲添加到列表中
{
声音效果。添加;
}
public void PlaySound(int s)//根据列表中的位置播放声音
{
如果(soundEffects.Count()>s)
{
SoundEffect ToPlaySound=soundEffects.ElementAt(s);
ToPlaySound.Play();
}
其他的
{
Console.WriteLine(“找不到声音”);
}
}
公共无效卷(int v)
{
SoundEffect.MasterVolume=(浮动)v/100;
}
}
我最终让需要访问SounneEngine的对象的构造函数作为参数传递给SoundEngine。
然后我可以在我需要它存在的对象中创建一个新的soundEngine并调用:this.soundEngine=soundEngine
成功地复制了soundEngine并让我使用它的功能。我最终让需要访问soundEngine的对象的构造函数作为参数传递soundEngine。 然后我可以在我需要它存在的对象中创建一个新的soundEngine并调用:this.soundEngine=soundEngine 成功地复制了声音引擎,让我使用它的功能
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;
}
}